我有一個程式,功能就是給選定檔案夾里的檔案重命名。
現在想添加一個功能,能夠動態顯示處理完了幾個檔案。
代碼如下:
# -*-coding:utf-8-*-
"""This is a free tool which easily adds sequence numbers to names of files
in a selected folder, just by clicking your mouse a few times.
Here is the usage:
1. Press the 'Click me' button.
2. Select a folder in the pop-up window.
3. Click 'Choose' to execute the operation or 'Cancel' to give it up.
4. Press 'Check' to make sure the operation has been completed successfully.
Note that operations on hidden files or sub-folders are neglected.
"""
import os
import tkinter as tk
from tkinter import filedialog
class ReNamer(tk.Tk):
def __init__(self):
super().__init__()
self.title("EasyReNamer V2.0")
self.n=0
self.value = tk.IntVar()
self.value.trace("r", self.show_result)
label_info = tk.Label(self, text="Please select a folder:")
btn_rename = tk.Button(self, text="Click Me", width=10,
highlightbackground='orange', command=self.rename)
btn_check = tk.Button(self, text="Check", width=10,
highlightbackground='darkblue', fg='white')
self.label_show = tk.Label(self)
label_info.grid(row=0, column=1)
btn_rename.grid(row=1, column=0, padx=50)
btn_check.grid(row=1, column=2, padx=50)
self.label_show.grid(row=2, column=1)
def rename(self):
folder_path = filedialog.askdirectory(title="EasyReNamer V2.0")
items_list = os.listdir(folder_path)
for item in items_list.copy():
item_path = folder_path + os.sep + item
if os.path.isdir(item_path) or item.startswith('.') or \ #macos下運行,win或者linux需要適當修改
item.startswith('~$'):
continue
else:
new_item_path = folder_path + os.sep + '(' + \
str(self.n+1) + ')' + item
os.rename(item_path, new_item_path)
self.n += 1
self.value.set(self.n)
print(self.value)
print(new_item_path)
def show_result(self, *args):
text = "{} file(s) renamed".format(self.value)
self.label_show.config(text=text)
if __name__ == "__main__":
root = ReNamer()
root.mainloop()
運行后,下面的Label始終無顯示,請問如何修改?
uj5u.com熱心網友回復:
這一行self.value.trace("r", self.show_result)
你要改成
self.value.trace("w", self.show_result)
uj5u.com熱心網友回復:
改了,還是有問題。我想可能需要加一個執行緒,但是問題仍然存在。
# -*-coding:utf-8-*-
"""This is a free tool which easily adds sequence numbers to names of files
in a selected folder, just by clicking your mouse a few times.
Here is the usage:
1. Press the 'Click me' button.
2. Select a folder in the pop-up window.
3. Click 'Choose' to execute the operation or 'Cancel' to give it up.
4. Press 'Check' to make sure the operation has been completed successfully.
Note that operations on hidden files or sub-folders are neglected.
"""
import os
import threading
import tkinter as tk
from tkinter import filedialog
class ReNamer(tk.Tk):
def __init__(self):
super().__init__()
self.title("EasyReNamer V2.0")
self.n = 0
label_info = tk.Label(self, text="Please select a folder:")
label_info.pack()
panel = tk.Frame()
btn_rename = tk.Button(panel, text="Click Me", width=10,
highlightbackground='orange', command=self.rename)
btn_rename.grid(row=0, column=0, padx=30)
btn_check = tk.Button(panel, text="Check", width=10,
highlightbackground='darkblue', fg='white')
btn_check.grid(row=0, column=1, padx=30)
panel.pack()
self.label_show = tk.Label(self)
self.label_show.pack()
def rename(self):
folder_path = filedialog.askdirectory(title="EasyReNamer V2.0")
items_list = os.listdir(folder_path)
thread = threading.Thread(target=root.display)
thread.start()
for item in items_list.copy():
item_path = folder_path + os.sep + item
if os.path.isdir(item_path) or item.startswith('.') or \
item.startswith('~$'):
continue
else:
new_item_path = folder_path + os.sep + '(' + \
str(self.n + 1) + ')' + item
os.rename(item_path, new_item_path)
self.n += 1
def display(self):
self.label_show.config(text="{} file(s) renamed".format(self.n))
print(self.n)
if __name__ == "__main__":
root = ReNamer()
root.mainloop()
uj5u.com熱心網友回復:
不知道你怎么改的,我試了沒問題啊
# -*-coding:utf-8-*-
"""This is a free tool which easily adds sequence numbers to names of files
in a selected folder, just by clicking your mouse a few times.
Here is the usage:
1. Press the 'Click me' button.
2. Select a folder in the pop-up window.
3. Click 'Choose' to execute the operation or 'Cancel' to give it up.
4. Press 'Check' to make sure the operation has been completed successfully.
Note that operations on hidden files or sub-folders are neglected.
"""
import os
import tkinter as tk
from tkinter import filedialog
class ReNamer(tk.Tk):
def __init__(self):
super().__init__()
self.title("EasyReNamer V2.0")
self.n = 0
self.value = tk.IntVar()
self.value.trace("w", self.show_result)
label_info = tk.Label(self, text="Please select a folder:")
btn_rename = tk.Button(self, text="Click Me", width=10,
highlightbackground='orange', command=self.rename)
btn_check = tk.Button(self, text="Check", width=10,
highlightbackground='darkblue', fg='white')
self.label_show = tk.Label(self)
label_info.grid(row=0, column=1)
btn_rename.grid(row=1, column=0, padx=50)
btn_check.grid(row=1, column=2, padx=50)
self.label_show.grid(row=2, column=1)
def rename(self):
folder_path = filedialog.askdirectory(title="EasyReNamer V2.0")
items_list = os.listdir(folder_path)
for item in items_list.copy():
item_path = folder_path + os.sep + item
if os.path.isdir(item_path) or item.startswith('.') or item.startswith('~$'):
continue
else:
new_item_path = folder_path + os.sep + '(' + str(self.n+1) + ')' + item
# os.rename(item_path, new_item_path)
self.n += 1
self.value.set(self.n)
print(self.value)
print(new_item_path)
def show_result(self, *args):
print(self.value)
text = "{} file(s) renamed".format(self.n)
self.label_show.config(text=text)
if __name__ == "__main__":
root = ReNamer()
root.mainloop()
uj5u.com熱心網友回復:
我運行了,還是沒有看到最后一行 file(s) renamed前面數字的變化(假設檔案夾有3個檔案,直接就顯示成3了,沒有看到1、2)uj5u.com熱心網友回復:
按你的需求,加個定時器比較方便,設定了固定的時間間隔或者這里可以根據檔案數量設定了這個值,3.0/檔案數,表示不管檔案多少,數字切換顯示總時長大約3秒,免得檔案多的時間等待時間過長
# -*-coding:utf-8-*-
"""This is a free tool which easily adds sequence numbers to names of files
in a selected folder, just by clicking your mouse a few times.
Here is the usage:
1. Press the 'Click me' button.
2. Select a folder in the pop-up window.
3. Click 'Choose' to execute the operation or 'Cancel' to give it up.
4. Press 'Check' to make sure the operation has been completed successfully.
Note that operations on hidden files or sub-folders are neglected.
"""
import os
import tkinter as tk
from tkinter import filedialog
from threading import Timer
class ReNamer(tk.Tk):
def __init__(self):
super().__init__()
self.title("EasyReNamer V2.0")
self.n = 0
label_info = tk.Label(self, text="Please select a folder:")
btn_rename = tk.Button(self, text="Click Me", width=10,
highlightbackground='orange', command=self.rename)
btn_check = tk.Button(self, text="Check", width=10,
highlightbackground='darkblue', fg='white')
self.label_show = tk.Label(self)
label_info.grid(row=0, column=1)
btn_rename.grid(row=1, column=0, padx=50)
btn_check.grid(row=1, column=2, padx=50)
self.label_show.grid(row=2, column=1)
def do_rename(self):
if len(self.file_arr) > 0:
item_path, new_item_path = self.file_arr.pop(0)
os.rename(item_path, new_item_path)
self.n += 1
text = "{} file(s) renamed".format(self.n)
self.label_show.config(text=text)
if len(self.file_arr) > 0:
# timer = Timer(0.2, self.do_rename)
timer = Timer(self.interval, self.do_rename)
timer.start()
def rename(self):
self.file_arr = []
folder_path = filedialog.askdirectory(title="EasyReNamer V2.0")
items_list = os.listdir(folder_path)
for item in items_list.copy():
item_path = folder_path + os.sep + item
if os.path.isdir(item_path) or item.startswith('.') or item.startswith('~$'):
continue
else:
new_item_path = folder_path + os.sep + '(' + str(self.n+1) + ')' + item
self.file_arr.append((item_path, new_item_path))
self.interval = 0 if len(self.file_arr) == 0 else 3.0 / len(self.file_arr)
if self.interval > 1:
self.interval = 1
print(self.interval)
self.n = 0
timer = Timer(0, self.do_rename)
timer.start()
if __name__ == "__main__":
root = ReNamer()
root.mainloop()
uj5u.com熱心網友回復:
我這樣改了一下,成功了。執行緒啟動的時機影響很大:在點擊按鈕就啟動執行緒,和點擊按鈕喚起rename再啟動執行緒,這兩者差得太遠了。雖然我不太理解為什么會造成這么大的差異。
# -*-coding:utf-8-*-
"""This is a free tool which easily adds sequence numbers to names of files
in a selected folder, just by clicking your mouse a few times.
Here is the usage:
1. Press the 'Click me' button.
2. Select a folder in the pop-up window.
3. Click 'Choose' to execute the operation or 'Cancel' to give it up.
4. Press 'Check' to make sure the operation has been completed successfully.
Note that operations on hidden files or sub-folders are neglected.
"""
import os
import time
import threading
import tkinter as tk
from tkinter import filedialog
class ReNamer(tk.Tk):
def __init__(self):
super().__init__()
self.title("EasyReNamer V2.0")
self.n = 0
label_info = tk.Label(self, text="Please select a folder:")
label_info.pack()
panel = tk.Frame()
btn_rename = tk.Button(panel, text="Click Me", width=10,
highlightbackground='orange',
command=threading.Thread(target=self.rename).start)
btn_rename.grid(row=0, column=0, padx=30)
btn_check = tk.Button(panel, text="Check", width=10,
highlightbackground='darkblue', fg='white')
btn_check.grid(row=0, column=1, padx=30)
panel.pack()
self.label_show = tk.Label(self)
self.label_show.pack()
def rename(self):
folder_path = filedialog.askdirectory(title="EasyReNamer V2.0")
items_list = os.listdir(folder_path)
for item in items_list.copy():
item_path = folder_path + os.sep + item
if os.path.isdir(item_path) or item.startswith('.') or \
item.startswith('~$'):
continue
else:
new_item_path = folder_path + os.sep + '(' + \
str(self.n + 1) + ')' + item
# os.rename(item_path, new_item_path)
self.n += 1
print(new_item_path)
time.sleep(2)
self.label_show.config(text="{} file(s) renamed".format(self.n))
print(self.n)
self.label_show.config(text="{} file(s) completed successfully".format(self.n))
if __name__ == "__main__":
root = ReNamer()
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/98064.html
