如何循环更新进度条? [英] How to update a progress bar in a loop?

查看:84
本文介绍了如何循环更新进度条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

循环更新 Tkinter 进度条的简单方法是什么?

What is the easy method to update Tkinter progress bar in a loop?

我需要一个没有太多混乱的解决方案,所以我可以轻松地在我的脚本中实现它,因为它对我来说已经非常复杂了.

I need a solution without much mess, so I can easily implement it in my script, since it's already pretty complicated for me.

假设代码是:

from Tkinter import *
import ttk


root = Tk()
root.geometry('{}x{}'.format(400, 100))
theLabel = Label(root, text="Sample text to show")
theLabel.pack()


status = Label(root, text="Status bar:", bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)

root.mainloop()

def loop_function():
    k = 1
    while k<30:
    ### some work to be done
    k = k + 1
    ### here should be progress bar update on the end of the loop
    ###   "Progress: current value of k =" + str(k)


# Begining of a program
loop_function()

推荐答案

这是一个持续更新 ttk 进度条的快速示例.您可能不想将 sleep 放在 GUI 中.这只是为了减慢更新速度,以便您可以看到它的变化.

Here's a quick example of continuously updating a ttk progressbar. You probably don't want to put sleep in a GUI. This is just to slow the updating down so you can see it change.

from Tkinter import *
import ttk
import time

MAX = 30

root = Tk()
root.geometry('{}x{}'.format(400, 100))
progress_var = DoubleVar() #here you have ints but when calc. %'s usually floats
theLabel = Label(root, text="Sample text to show")
theLabel.pack()
progressbar = ttk.Progressbar(root, variable=progress_var, maximum=MAX)
progressbar.pack(fill=X, expand=1)


def loop_function():

    k = 0
    while k <= MAX:
    ### some work to be done
        progress_var.set(k)
        k += 1
        time.sleep(0.02)
        root.update_idletasks()
    root.after(100, loop_function)

loop_function()
root.mainloop()

这篇关于如何循环更新进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆