简体   繁体   English

Python Tkinter标签延迟

[英]Python Tkinter Label Delay

This maybe a silly question to ask. 这可能是个愚蠢的问题。 I have a label in Tkinter GUI and i want it to be updated through time. 我在Tkinter GUI中有一个标签,我希望它能够随时更新。

Example: 例:

Msglabel=Tkinter.Label(... text="")

Msglabel.Cofigure(text=" EXAMPLE!")

Wait(5sec)

Msglabel.Configure(text=" NEW EXAMPLE!")

I've read about the after() method but im looking for something like Wait. 我已经阅读了关于after()方法但我正在寻找像Wait这样的东西。

You'll need to hand over control to Tkinter during your wait period since Tkinter updates the UI in a single threaded loop. 由于Tkinter在单线程循环中更新UI,因此您需要在等待期间将控制权移交给Tkinter。

Sleeping between the configure calls will hang the UI. 在配置调用之间休眠将挂起UI。

As you mentioned, after is the method you want. 正如您所提到的, after是您想要的方法。 Try something like this: 尝试这样的事情:

try:
    import Tkinter as tkinter  # Python 2
except ImportError:
    import tkinter  # Python 3
import itertools


class MyApplication(object):
    def __init__(self):
        # Create and pack widgets
        self.root = tkinter.Tk()
        self.label = tkinter.Label(self.root)
        self.button = tkinter.Button(self.root)
        self.label.pack(expand=True)
        self.button.pack()

        self.label['text'] = 'Initial'
        self.button['text'] = 'Update Label'
        self.button['command'] = self.wait_update_label

        # Configure label values
        self.label_values = itertools.cycle(['Hello', 'World'])

    def launch(self):
        self.root.mainloop()

    def wait_update_label(self):
        def update_label():
            value = next(self.label_values)
            self.label['text'] = value

        update_period_in_ms = 1500
        self.root.after(update_period_in_ms, update_label)
        self.label['text'] = 'Waiting...'


if __name__ == '__main__':
    app = MyApplication()
    app.launch()

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

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