在tkinter中遇到进度条问题

时间:2016-07-13 00:08:13

标签: python tkinter progress-bar

这是我的代码

janela_barra=Toplevel()
janela_barra.title("Processando...")
janela_barra["bg"]="light grey"
janela_barra.minsize(width=400, height=80)
janela_barra.maxsize(width=400, height=80)
comprimento = janela_barra.winfo_screenwidth()
altura = janela_barra.winfo_screenheight()
x = (comprimento/2)-(400/2)
y = (altura/2)-(80/2)
janela_barra.geometry("400x80+%d+%d" % (x, y))
janela_barra.iconbitmap('icon6.ico')
pb = ttk.Progressbar(janela_barra, orient=HORIZONTAL, length=200, mode='determinate')
pb.grid(column=0,row=0)
pb["value"]=0 
pb["maximum"]=100

然后在里面有一个for:

for i in range(0,tamanho):
    pb["value"]=i
    pb.update()

其中tamanho是for的迭代次数(现在是420),但事实是,它只打开一个窗口然后关闭,我无法看到进度条实际上起作用,无论我有多少增加tamanho

1 个答案:

答案 0 :(得分:0)

只需在循环中添加sleep(来自模块time),您就会看到进展情况。这是一个例子:

from tkinter import Tk, Button, Toplevel
from tkinter import ttk
from time import sleep

root = Tk()

def fct_run_for():
    top=Toplevel(root)
    top.title("Progression")
    pb = ttk.Progressbar(top, orient="horizontal", length=200, mode='determinate')
    pb.grid(column=0,row=0)
    pb["value"]=0 
    pb["maximum"]=100
    for i in range(100):
        pb["value"] += 1
        pb.update()
        sleep(0.1)

Button(root, text="Run", command=fct_run_for).pack()
root.mainloop()