python - Tkinter 进度条挂起程序并且不继续

标签 python tkinter progress

我尝试使用线程实现 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/

相关文章:

python - 如何使用python flask解决未读结果错误

python - 需要帮助优化代码,无法决定使用 dataframe 进行排序还是 mysql

python - 如何更改 Python Tkinter OptionMenu 中单词的颜色

python - 如何将Python代码附加到exe文件

java - 获取ObjectOutputStream/ObjectInputStream的进度

python - 寻找 i18ndude 的替代品,或者改进版

python - 我们如何在 django-hstore 中查询嵌套字典?

python - 理解 python 中的 lambda 并使用它来传递多个参数

php - 如何让我的php程序一直在linux下运行?

user-interface - Javafx 中的 ProgressBar 不会在 onAction block 中更新