tkinter标签在睡前不会更新

时间:2018-12-12 22:23:59

标签: python tkinter

在下面的代码中,单击BadButton永远不会显示“ New BadButton文本”,它会等待两秒钟,然后显示“ Third BadButton文本” 我如何说服脚本同时显示两条消息。 使用Python 2.7.10

import os, sys
import time
import Tkinter
from Tkinter import *

def GoodButton():
   global labeltext

   time.sleep(2)
   labeltext.set('New GoodButton text')

def BadButton():
   global labeltext

   labeltext.set('New BadButton text')
   time.sleep(2)
   labeltext.set('Third BadButton text')

# GUI construction
root = Tkinter.Tk()
root.title("Label Problem")
# set background
back = Tkinter.Frame(master=root, width=375, height=125, bg='white')
back.pack()

B1 = Tkinter.Button(root,text ="Test GoodButton", command =     GoodButton)
B1.pack()
B1.place(height=25, width=100, x = 25, y = 25)   # default x,y = 0,0

B2 = Tkinter.Button(root,text ="Test BadButton", command = BadButton)
B2.pack()
B2.place(height=25, width=100, x = 150, y = 25)

# add a label to show progress
labeltext = Tkinter.StringVar(value = 'Ready')
L1 = Tkinter.Label(root,textvariable = labeltext)
L1.pack()
L1.place(height=25, width=175, x = 150, y = 75)

# MAIN
root.mainloop()

1 个答案:

答案 0 :(得分:0)

您可以使用after()做您想做的事情:

def updateLabel(txt):
  labeltext.set(txt)

def GoodButton():
  root.after(2000, updateLabel, 'New GoodButton text')

def BadButton():
  updateLabel('New BadButton text')
  root.after(2000, updateLabel, 'Third BadButton text')