我尝试使用线程实现 Tkinter 进度条,只是为了查看程序何时运行,并在程序结束时关闭进度条。
import tkinter
import ttk
import time
import threading
def task(root):
ft = ttk.Frame()
ft.pack(expand=True, fill=tkinter.BOTH, side=tkinter.TOP)
pb_hD = ttk.Progressbar(ft, orient='horizontal', mode='indeterminate')
pb_hD.pack(expand=True, fill=tkinter.BOTH, side=tkinter.TOP)
pb_hD.start(50)
root.mainloop()
def process_of_unknown_duration(root):
time.sleep(5)
root.destroy()
def pBar():
root = tkinter.Tk()
t1=threading.Thread(target=process_of_unknown_duration, args=(root,))
t1.start()
task(root) # This will block while the mainloop runs
t1.join()
if __name__ == '__main__':
pBar()
#some function
我的问题是,一旦进度条启动,程序就会挂起并且不会执行任何其他操作。有什么提示吗?
最佳答案
这是因为您调用了 root.mainloop()
is blocking the execution of your code 。它基本上代表了 UI 的循环。您可能想查看this answer用于通过按钮启动的进度条。
关于python - Tkinter 进度条挂起程序并且不继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55404092/