R. Kap
R. Kap

Reputation: 619

How to disable/enable a specific tkinter button under certain conditions?

I have created the following button in a tinter window:

resetall = Button(text = "Clear ALL", command = confirmation)
resetall.pack(side = "left")

This button "Clears" the canvas that the user is drawing on with the Python turtle, but I want this button to be enabled under CERTAIN CONDITIONS, such as if one function is running or not. I have tried this:

if draw.drawing == False:
   resetall.config(state = DISABLED)
elif draw.drawing == True:
   resetall.config(state = NORMAL)

to enable the button ONLY when the "draw" function is true, otherwise disable it. However, it does not seem to work, as even when the draw function becomes true, it does not get enabled. What am I doing wrong here? Any help is much appreciated! :)

Upvotes: 1

Views: 10314

Answers (2)

R. Kap
R. Kap

Reputation: 619

Twas a very simple fix. All I had to do was make resetall a global variable, and then assign resetall.config(state = ACTIVE) to draw.

Upvotes: 1

Tanya Aggarwal
Tanya Aggarwal

Reputation: 3

Make the variable containing a button to be a global variable and then the state of the button can be changed by using button_name.config(state=ACTIVE) or button_name.config(state=DISABLED).

Remember- once the button has been disabled you will not be able to activate it if you have changed the state of the button to disabled inside that same function. You would need another function to activate your previous button once disabled

Upvotes: 0

Related Questions