我用tkinter制作了进度条。我正在尝试让进度条跟踪复制进度。有没有办法查看自动复制的进度并将其用于进度条?
我知道可以通过计算源目录的大小然后检查目标目录的大小来解决。它确实有效,但是我无法检查robocopy是否跳过了任何内容,然后进度条将永远持续下去。
我将此代码用于robocopy:
subprocess.call(["robocopy", os.path.dirname(file_path), new_destination, filename[0], "/V", "/ETA", "/W:3", "/R:3",
"/TEE", "/LOG+:" +
new_destination + "copy.log"])
如果可能的话,我想用robocopy输出中的进度更新进度条,如果不可能,那是一种检查robocopy是否完成的方法。
谢谢!
答案 0 :(得分:0)
首先,当我按下一个按钮并选择了要复制的文件/目录时,我检查了它们的总大小。我将其保存,并将进度条的最大值设置为总大小。然后执行start_calc方法。
for item in self.file_and_directory_list:
folder = ""
my_file = ""
if os.path.isdir(item):
folder = item
else:
my_file = item
if folder != "":
print("folder")
for (path, dirs, files) in os.walk(folder):
for file in files:
filename = os.path.join(path, file)
folder_size += os.path.getsize(filename)
else:
folder_size += os.path.getsize(item)
if os.path.exists(new_destination):
for (path, dirs, files) in os.walk(new_destination):
for file in files:
filename = os.path.join(path, file)
folder_size += os.path.getsize(filename)
processing_bar_copy_files["maximum"] = folder_size
processing_bar_copy_files["value"] = 0
threading.Thread(target=self.start_calc, args=(folder_size, new_destination)).start()
start_calc方法,我在其中计算目标目录的大小,并检查它是否比源目录小。如果较小,则进度条的值将设置为目标目录的大小:
def start_calc(self, source_folder_size, new_destination):
folder = new_destination
dest_folder_size = 0
try:
for (path, dirs, files) in os.walk(folder):
for file in files:
filename = os.path.join(path, file)
dest_folder_size += os.path.getsize(filename)
except Exception as error:
tkinter.messagebox.showerror(f"Er is iets fout gegaan:\n{error}")
finally:
if processing_bar_copy_files["value"] < processing_bar_copy_files["maximum"]:
processing_bar_copy_files["value"] = dest_folder_size
progress_percentage = (processing_bar_copy_files["value"] / processing_bar_copy_files["maximum"]) * 100
progress_text_var.set(str("{:.1f}".format(progress_percentage) + "%"))
self.size = dest_folder_size
window.after(500, self.start_calc, source_folder_size, new_destination)
如果文件复制完成,进度条将被删除并停止,并将按钮放回原位。