Reputation: 353
My tkinter gui starts to freeze when I click on somewhere else. Is there a way to prevent that?
Here's my code:
#=========================
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()
Note: The label for display is the Button itself, so I name it 'label'.
Upvotes: 0
Views: 916
Reputation: 49330
You're doing several strange things in your code:
time.sleep
in a Tkinter applicationLabel
Tkinter widget)command
random
module around and expecting it to evaluate to an integerif True:
)random
to refer to both the random
module and a passed argument, at the same timeafter
after
, allowing you to schedule many callsif
structure to choose a random string instead of using random.choice
after
call with the result of a function call (display(random)
) instead of the function itselfThat's not necessarily a complete list.
The following fixes the above issues.
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()
Upvotes: 4