【发布时间】:2022-11-17 00:39:01
【问题描述】:
import time
import tkinter
global win
def func1():
global win
win = tkinter.Tk()
win.geometry("300x200")
win.configure(bg='blue')
time.sleep(5)
button_win = tkinter.Button(win,text='Go',command=func2)
button_win.pack()
print('mainloop')
win.mainloop()
def func2():
print("func2")
global win
win.configure(bg = 'green')
time.sleep(5)
print("in func1")
time.sleep(5)
print("func3 call")
func3()
def func3():
global win
time.sleep(5)
win.configure(bg = 'yellow')
func1()
控制台中的输出
mainloop
(I click on 'Go' button)
func2
in func1
func3 call
我已经使用 time.sleep(5) 来查看更改是否反映在窗口中。 窗口以蓝色打开。单击“开始”按钮后,几秒钟后,它变为黄色。但是为什么当它进入函数'func2'时它没有变成绿色。我怎么做?
【问题讨论】:
-
这回答了你的问题了吗? tkinter and time.sleep
-
不,但感谢@Thingamabobs。我使用 time.sleep(5) 只是为了查看窗口是否变绿。否则这里不需要 time.sleep(5)。它可以从代码中删除。
-
与问题无关,但切勿将
time.sleep()与 tkinter 一起使用,请使用 tkinter.after()method。 -
谢谢@TrooperZ,我会记住这一点。
-
我一直评论。睡眠(5),仍然窗口没有更新为绿色
标签: python user-interface tkinter