简体   繁体   English

如何更新 tkinter 上的 ttk 进度条

[英]How to update the ttk progress bar on tkinter

I've got a program that does some simple webscraping.我有一个程序可以做一些简单的网页抓取。 I'm working on giving it to my coworkers to use, but they are unfamiliar with code so I'd like to make a GUI.我正在努力将它提供给我的同事使用,但他们不熟悉代码,所以我想制作一个 GUI。 I've started using tkinter, and I'm currently working on making a progress bar showing where the program is at (it can take up to a couple hours to run).我已经开始使用 tkinter,我目前正在制作一个进度条,显示程序所在的位置(运行可能需要几个小时)。 My problem is that I can't seem to get the progress bar to update, and all the online sources use Tkinter, which is an old version.我的问题是我似乎无法让进度条更新,并且所有在线资源都使用Tkinter,这是一个旧版本。 Here is my code:这是我的代码:

I've tried updating progressBar['value'] to whatever number I want, but that hasn't been working.我试过将 progressBar['value'] 更新为我想要的任何数字,但这并没有奏效。

from tkinter import *
from tkinter import ttk
import time


def clicked(progressBar): # runs a couple functions and updates the progress bar when the button is clicked
    num = 0
    for item in range(5):
        # functions go here
        num += 10
        progressBar['value'] = num
        time.sleep(2)

window = Tk()
window.title("Welcome to my app")
window.geometry('600x400')

progressBar = ttk.Progressbar(window, orient='horizontal', length=300, mode='determinate', maximum=100, value=0)
progressBar.grid(columnspan=3, row=2, sticky=(W, E))

btn = Button(window, text="Click me!", command=clicked(progressBar))
btn.grid(column=1, row=1)

window.mainloop()

The tkinter window doesn't open up until 10 seconds after I run the program, and it has the progress bar already at 50% full. tkinter 窗口在我运行程序 10 秒后才打开,并且进度条已经达到 50%。 I'd like for the bar to slowly increment up, AFTER the button has been clicked.在单击按钮后,我希望条形图慢慢增加。 Any advice would be helpful!任何意见将是有益的! Thank you!谢谢!

There are two problems with the code:代码有两个问题:

  1. command=clicked(progressBar) indeed calls the function promptly.So simply use command=clicked . command=clicked(progressBar)确实会立即调用该函数。所以只需使用command=clicked There is no need to pass progressBar as the argument since the function gets it from global scope.无需将 progressBar 作为参数传递,因为该函数从全局范围获取它。
  2. while the function clicked() is running, GUI freezes.当函数clicked()运行时,GUI 冻结。 After 5*2sec, progressBar updates to 5*10 abruptly. 5*2 秒后, progressBar突然更新为 5*10。 To update the widgets in a loop, use update_idletastk method:要循环更新小部件,请使用update_idletastk方法:
import tkinter as tk
from tkinter import ttk
import time


def clicked():
    num = 0
    for item in range(5):
        num += 10
        progressBar['value'] = num
        window.update_idletasks()
        time.sleep(2)

window = tk.Tk()

progressBar = ttk.Progressbar(window, orient='horizontal', length=300, mode='determinate', maximum=100, value=0)
progressBar.grid(columnspan=3, row=2, sticky=(tk.W, tk.E))

btn = tk.Button(window, text="Click me!", command=clicked)
btn.grid(column=1, row=1)

window.mainloop()

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

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