2013-03-29 13 views
0

C'est peut-être une question idiote à poser. J'ai un label dans Tkinter GUI et je veux qu'il soit mis à jour au fil du temps.Python Tkinter Label Delay

Exemple:

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

Msglabel.Cofigure(text=" EXAMPLE!") 

Wait(5sec) 

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

J'ai lu sur la méthode after() mais je cherche quelque chose comme attente.

Répondre

2

Vous devrez remettre le contrôle à Tkinter pendant votre période d'attente, car Tkinter met à jour l'interface utilisateur dans une seule boucle filetée.

Dormir entre les appels de configuration bloquera l'interface utilisateur.

Comme vous l'avez mentionné, after est la méthode que vous voulez. Essayez quelque chose comme ceci:

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()