简体   繁体   English

Tkinter / TTK:如何确定ProgressBar是否达到最大值?

[英]Tkinter/TTK: How to determine if a ProgressBar reaches the maximum value?

I'm trying to make simple program that prints "done" to the console when a progress bar reaches the maximum value using ttk. 我正在尝试制作一个简单的程序,当使用ttk达到进度条的最大值时,该程序将向控制台打印“完成”。

example: 例:

from tkinter import *
import tkinter.ttk

root = Tk()

pb = tkinter.ttk.Progressbar(root, orient=HORIZONTAL, length=200, mode='determinate')
pb.pack()
pb.start()

if pb['value'] == 100: #This isn't correct it's just an example.
    pb.stop()
    print("Done")

root.mainloop()

I'm currently using python 3.5.2, please try to avoid classes and objects, It's a bit hard for me to understand them. 我目前正在使用python 3.5.2,请尝试避免使用类和对象,这对我来说有点难以理解。

You can update the value yourself by instructing a function to be called every 100ms or so, like this: 您可以通过指示每100ms左右调用一次函数来自己更新值,如下所示:

from tkinter import *
import tkinter.ttk

root = Tk()

pb = tkinter.ttk.Progressbar(root, orient=HORIZONTAL, length=200, mode='determinate')
pb.pack()

def task():
    pb['value'] += 1
    if pb['value'] >= 99:
        print("Done")
    else:
        root.after(100, task) # Tell the mainloop to run "task()" again after 100ms

# Tell the mainloop to run "task()" after 100ms
root.after(100, task)

root.mainloop()

You typically don't start() the progressbar in determinate mode because you're supposed to update the value yourself. 通常,您不应该在确定模式下使用start()进度条,因为您应该自己更新值。 In indeterminate mode, the bar bounces back and forth to imply something is happening, which is why you need to start() it. 在不确定模式下,该条会来回反弹以暗示发生了某些事情,这就是为什么需要启动它的原因。

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

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