Reputation: 125
below you can see my code:
from tkinter import *
from tkinter import ttk
import time
class MainWindow:
def __init__(self):
self.root = Tk()
self.root.geometry("300x200")
self.root.title("Test")
self.StartButton=ttk.Button(self.root, text="Start", command=self.Start)
self.StartButton.pack()
self.root.mainloop()
def Start(self):
self.StartButton.config(state = "disable")
#input()
time.sleep(10)
self.StartButton.config(state = "enable")
app = MainWindow()
when you click on the OK
button, the script doesn't do nothing for 10 seconds, and during these seconds, the button should be disabled, but it doesn't happen. it happens only if you block the script using for example the input()
function.
I really don't understand this weird behaviour. how can I solve this issue? it seems that the button is not able to disable itself duting the process, but the instruction to disable it, comes first than the process! so what is the issue?
Upvotes: 1
Views: 1014
Reputation: 36652
The options are 'disabled'
and 'normal
' (although disable
also works, thanks to @Saad in the comments)
You also need to call root.update()
for the changes to take place.
def Start(self):
self.StartButton.config(state="disabled")
self.root.update()
time.sleep(2)
self.StartButton.config(state="normal")
Alternatively (as also proposed in the comments by @acw1668), you should probably use root.after
to reset your button after some time; this prevents the blocking effect of sleep
, and makes the call to root.update()
unnecessary:
def Start(self):
self.StartButton.config(state="disabled")
self.root.after(2000, lambda: self.StartButton.config(state="normal"))
Upvotes: 5