简体   繁体   English

手动进度条python

[英]Manual progress bar python

Im trying to make a code so that it displays all numbers from 1 to 100 to show as its loading something. 我正在尝试编写代码,以便显示从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)

but it seems like it doesnt want to show each number untill the loop is complete, at the end it just shows a 100. Any suggestions on how to fix it? 但是似乎不希望在循环完成前显示每个数字,最后只显示100。如何解决此问题的任何建议?

tkinter has mainloop() which runs all the time and does many thing - ie. tkinter具有mainloop()它运行所有的时间,做了很多事情-即。 it updates data in widget, redraws widgets, executes your function. 它会更新小部件中的数据,重绘小部件,执行您的功能。 When mainloop() executes your function then it can't updates/redraws widgets till your function stop running. mainloop()执行函数时,直到函数停止运行,它才能更新/重绘窗口小部件。 You can use root.update() in your function to force tkinter to update/readraw widgets. 您可以在函数中使用root.update()强制tkinter更新/重新绘制窗口小部件。

Or you can use 或者你可以使用

root.after(miliseconds, function_name) 

to periodically execute function which will update Label . 定期执行将更新Label功能。 And between executions tkinter will have time to update/redraw widgets. 在两次执行之间, tkinter将有时间更新/重绘窗口小部件。

Examples which use after to update time in Label : Label更新时间after使用的示例:

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


BTW: tkinter has ttk.Progressbar BTW: tkinterttk.Progressbar

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM