tkinter python切换小部件

时间:2014-12-22 18:05:32

标签: python button tkinter labels

from tkinter import *
from time import sleep
root = Tk()
l = Label ( root , text = "bla" )
b = Button ( root , text = "python je glup" )

def glupsam ():
   b.grid_forget()
   b.update_idletasks()
   l.grid ( row = 0 , column = 0 )
   l.update_idletasks()
   sleep ( 2 )
   l.grid_forget()
   l.update_idletasks()
   b.grid(row = 0 , column = 0)

b.grid( row = 0 , column = 0 )
b.configure ( command = glupsam )

root.mainloop()

我是python和tkinter的新手。 为什么这个代码每按一次按钮就不会在网格(0,0)上切换标签和按钮,但只是第一次

1 个答案:

答案 0 :(得分:0)

尝试使用after恢复按钮,而不是使用sleep

from tkinter import *
from time import sleep
root = Tk()
l = Label ( root , text = "bla" )
b = Button ( root , text = "python je glup" )

def glupsam ():
    b.grid_forget()
    b.update_idletasks()
    l.grid ( row = 0 , column = 0 )
    l.update_idletasks()
    def f():
        l.grid_forget()
        l.update_idletasks()
        b.grid(row = 0 , column = 0)
    root.after(2000, f)

b.grid( row = 0 , column = 0 )
b.configure ( command = glupsam )

root.mainloop()