platinumtea7
platinumtea7

Reputation: 17

How can you disable a button mid-program in tkinter?

Let's say I have a button, button = Button(root, text="Hello", command = somecommand) and I want to disable it if some condition is true mid-program. Here is my code:

from tkinter import *
root=Tk()
def hi():
    print("hi")
button=Button(root,text="Say hello",command=hi)
button.pack()
while 1:
    root.update_idletasks()
    root.update()
    if 1==1:
        #disable button here

Thanks!

Upvotes: 0

Views: 1456

Answers (1)

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19144

Bookmark this reference (not perfect, but extremely helpful). The Button page says "state: Set this option to tk.DISABLED to gray out the button and make it unresponsive. Has the value tk.ACTIVE when the mouse is over it. Default is tk.NORMAL. ". In other words, button['state'] = DISABLED and button['state'] = NORMAL will de-activate and restore button.

Upvotes: 3

Related Questions