我用tkinter做了一个进度条。我正在尝试让进度条跟随复制进度。有没有办法查看机器人复制的进度并将其用于进度条?
我知道有某种解决方案,通过计算源目录的大小,然后检查目标目录的大小。它确实有效,但我无法检查机器人复制是否跳过了任何内容,然后进度条将永远持续下去。
我使用此代码进行机器人复制:
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 是否已完成的方法。
提前感谢!
首先 当我按下一个按钮并选择要复制的文件/目录时,我会检查它组合的大小。我保存它并将进度条的最大值设置为总大小。然后执行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)
如果文件复制完成,则会删除并停止进度条,并将按钮放回原处。