0
当我点击其他地方时,我的tkinter gui开始冻结。有没有办法阻止?如何防止tkinter冻结,当我点击其他地方?
这里是我的代码:
#=========================
from tkinter import *
from time import sleep
import random
#=====================
root=Tk()
root.title("Wise Words")
root.geometry("500x180+360+30")
root.resizable(0,0)
root.call("wm", "attributes", ".", "-topmost", "1")
#===================
def display(random):
if random == 1:
return "Be wise today so you don't cry tomorrow"
elif random == 2:
return "Frustration is the result of failed expectations"
elif random == 3:
return "Wishes are possibilities. Dare to make a wish"
if True:
sleep(4)
r=random.randint(1,3)
sentence=display(r)
label.configure(text=str(sentence))
label.update_idletasks()
root.after(5000, display(random))
#==================
def Click(event):
display(random)
#======================
label=Button(root, fg="white", bg="blue", text="Click to start!",
font=("Tahoma", 20, "bold"), width=40, height=4,
wraplength=400)
label.bind("<Button-1>", Click)
label.pack()
#================
root.mainloop()
注:显示的标签是按钮本身,所以我将其命名为“标签”。
你为什么使用'time.sleep'和'root.after'?另外,除了引用它,并且期望它等于一个整数(它永远不会),你从来不会对'random'模块做任何事情。 – TigerhawkT3
当我删除时间模块时,即使root.after设置为5000,它也会立即开始循环。这不是我想要的。我使用随机模块随机化显示。请参阅r = random.randint(1,3),然后使用sentence = display(r)。 –
哦,现在我看到你在用'random'做些什么。但是,您立即在递归调用中使用它,然后通过'after'调用该函数_again_。所以,你有一个有用的数字,你在有问题的'sleep'之后递归地发送,然后通过适当的'after'发送一个无用的值。这是不好的。 – TigerhawkT3