gpt4 book ai didi

python - 如何从主窗口启动 TKinter 进度条

转载 作者:行者123 更新时间:2023-12-01 03:54:07 24 4
gpt4 key购买 nike

我在创建进度条的方法中有代码:

def progress_bar(self, message = None, icon_path = None, icon_name = None, size = 0, file_location = None, interval = 100):
#size: in bytes of download
#file_location: location of the download
#interval: frequency, in ms, of how often to poll the file for progress
self.find_icon(icon_path, icon_name)
self.text_label = tk.Label(text = message)
self.set_colors(self.text_label)
self.set_colors(self.image_label)
self.image_label.grid(row = 1, column = 1, sticky = 'NSEW')
self.text_label.grid(row = 1, column = 2, sticky = 'NSEW')
self.progress = ttk.Progressbar(self, orient="horizontal", length=(self.xp/3), mode="determinate")
self.progress.grid(row = 1, column = 3, sticky = 'NSEW')
self.progress["value"] = 0
self.progress["maximum"] = size
self.auto_resize(1, 3)
self.progress.start(interval)
self.after(1, self.check_for_completion(file_location))
self.mainloop()

def check_for_completion(self, file_location):
#while (self.progress["value"] < self.progress["maximum"]):
self.progress["value"] = os.stat(file_location).st_size
print self.progress["value"]

if (self.progress["value"] >= self.progress["maximum"]):
self.text_label = tk.Label(text = message + ": Completed")
#force a repaint and wait for the user to see it
self.text_label.grid(row = 1, column = 2)
time.sleep(30)
self.choice(True)
self._delete_window

通过健全性测试,我在同一脚本中有一个 __ main __ 方法,我想做的是创建一个临时文件,启动进度条,然后开始将字符放入临时文件中。不幸的是,我找不到以非阻塞方式启动进度栏的方法。

我尝试像这样使用 python Multiprocessing:

p = Process(target = frame4.progress_bar, kwargs = kwarrgh)
p.start()
p.join()

并且非常生气;)消息说

Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.

注意:本练习的重要部分是从主窗口在后台启动 Tkinter 窗口,而不是相反。我发现了很多关于后者的问题,但没有发现前者。

答案不需要是线程安全的,以防存在有效的多线程而不是多处理方法。

最佳答案

只需使用 after() 继续调用函数 check_for_completion 即可,直到流程完成。

def check_for_completion(self, file_location):
... # your code

if (self.progress["value"] >= self.progress["maximum"]):
... # you code
else: # 'value' is not bigger then maximum so we run the function again.
self.after(10, lambda: self.check_for_completion(file_location))

请注意我如何使用 after 方法来调用带参数的函数。您也应该在您的线路上这样做:

#self.after(1, self.check_for_completion(file_location))  # Not like this
self.after(1, lambda: self.check_for_completion(file_location)) # like this

关于python - 如何从主窗口启动 TKinter 进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37800007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com