当我点击其他地方时如何防止 tkinter 冻结?
how to prevent tkinter from freezing when I click somewhere else?
当我点击其他地方时,我的 tkinter gui 开始冻结。有什么办法可以避免吗?
这是我的代码:
#=========================
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()
注意:显示的标签是Button本身,所以我将其命名为'label'。
您在代码中做了一些奇怪的事情:
- 在 Tkinter 应用程序中使用
time.sleep
- 将按钮称为标签(有一个
Label
Tkinter 小部件)
- 将鼠标左键绑定到一个按钮而不是只给按钮一个
command
- 传递
random
模块并期望它的计算结果为整数
- 将字符串返回给按钮
- 使用无条件分支语句(
if True:
)
- 用参数名称屏蔽模块名称
- 期望名称
random
同时引用 random
模块 和 传递的参数
- 在已经调用
after
的函数中进行递归调用
- 将按钮绑定到一个已经使用
after
安排对自身的调用的函数,允许您安排许多调用
- 使用
if
结构来选择随机字符串而不是使用 random.choice
- 使用函数调用 (
display(random)
) 的结果而不是函数本身安排 after
调用
这不一定是完整列表。
以下解决了上述问题。
from tkinter import *
import random
def display():
strings = ("Be wise today so you don't cry tomorrow",
"Frustration is the result of failed expectations",
"Wishes are possibilities. Dare to make a wish")
button.config(text=random.choice(strings))
root.after(5000, display)
def click(event=None):
button.config(command='')
display()
root=Tk()
root.title("Wise Words")
root.geometry("500x180+360+30")
root.resizable(0,0)
root.call("wm", "attributes", ".", "-topmost", "1")
button = Button(root, fg="white", bg="blue", text="Click to start!",
font=("Tahoma", 20, "bold"), width=40, height=4,
wraplength=400, command=click)
button.pack()
root.mainloop()
当我点击其他地方时,我的 tkinter gui 开始冻结。有什么办法可以避免吗?
这是我的代码:
#=========================
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()
注意:显示的标签是Button本身,所以我将其命名为'label'。
您在代码中做了一些奇怪的事情:
- 在 Tkinter 应用程序中使用
time.sleep
- 将按钮称为标签(有一个
Label
Tkinter 小部件) - 将鼠标左键绑定到一个按钮而不是只给按钮一个
command
- 传递
random
模块并期望它的计算结果为整数 - 将字符串返回给按钮
- 使用无条件分支语句(
if True:
) - 用参数名称屏蔽模块名称
- 期望名称
random
同时引用random
模块 和 传递的参数 - 在已经调用
after
的函数中进行递归调用
- 将按钮绑定到一个已经使用
after
安排对自身的调用的函数,允许您安排许多调用 - 使用
if
结构来选择随机字符串而不是使用random.choice
- 使用函数调用 (
display(random)
) 的结果而不是函数本身安排after
调用
这不一定是完整列表。
以下解决了上述问题。
from tkinter import *
import random
def display():
strings = ("Be wise today so you don't cry tomorrow",
"Frustration is the result of failed expectations",
"Wishes are possibilities. Dare to make a wish")
button.config(text=random.choice(strings))
root.after(5000, display)
def click(event=None):
button.config(command='')
display()
root=Tk()
root.title("Wise Words")
root.geometry("500x180+360+30")
root.resizable(0,0)
root.call("wm", "attributes", ".", "-topmost", "1")
button = Button(root, fg="white", bg="blue", text="Click to start!",
font=("Tahoma", 20, "bold"), width=40, height=4,
wraplength=400, command=click)
button.pack()
root.mainloop()