【问题标题】:Manual progress bar python手动进度条python
【发布时间】:2016-10-14 10:26:20
【问题描述】:

我正在尝试编写一个代码,以便它显示从 1 到 100 的所有数字,以显示它正在加载的东西。

for i in range(101):
        self.new = Label(self.label_progress, text=i)
        time.sleep(1)
        self.new.place(in_= self.label_progress)
        if i == 100:
            self.new1=Label(self.label_progress, text="the download is complete")
            self.new1.place(in_=self.label_progress, x=50)

但它似乎在循环完成之前不想显示每个数字,最后它只显示 100。关于如何修复它的任何建议?

【问题讨论】:

标签: python loops tkinter


【解决方案1】:

tkintermainloop(),它一直在运行并且做很多事情 - 即。它更新小部件中的数据,重绘小部件,执行您的功能。当mainloop() 执行您的函数时,它无法更新/重绘小部件,直到您的函数停止运行。您可以在函数中使用 root.update() 来强制 tkinter 更新/读取小部件。

或者你可以使用

root.after(miliseconds, function_name) 

定期执行更新Label的函数。在执行之间tkinter 将有时间更新/重绘小部件。

使用after 更新Label 中时间的示例:

https://github.com/furas/my-python-codes/tree/master/tkinter/timer-using-after


顺便说一句: tkinterttk.Progressbar

示例代码:https://stackoverflow.com/a/24770800/1832058

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-10
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    相关资源
    最近更新 更多