Disable Tkinter button while executing command Disable Tkinter button while executing command tkinter tkinter

Disable Tkinter button while executing command


I prefer to utilize Tkinter's "after" method, so other things can be done while the 5 seconds are counting down. In this case that is only the exit button.

from Tkinter import *##import timefrom functools import partialtop = Tk()def Run(object):    if object["state"] == "active":        object["state"] = "disabled"        object.after(5000, partial(Run, object))    else:        object["state"] = "active"    print object["state"]b1 = Button(top, text = 'RUN')b1.pack()## pass b1 to function after it has been createdb1["command"] = partial(Run, b1)b1["state"]="active"Button(top, text="Quit", command=top.quit).pack()top.mainloop()


Use pack_forget() to disable and pack() to re-enable. This causes "pack" window manager to temporarily "forget" it has a button until you call pack again.

from Tkinter import *import timetop = Tk()def Run(object):    object.pack_forget()    print 'test'    time.sleep(5)    object.pack()b1 = Button(top, text = 'RUN', command = lambda : Run(b1))b1.pack()top.mainloop()


You need

object.config(state = 'disabled')b1.update()time.sleep(5)object.config(state = 'normal')b1.update()

to update the button and pass execution back to Tkinter.