1

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! :)

7
  • 1
    you have to change state any time you change draw.drawing Commented Jan 9, 2016 at 0:37
  • 2
    It doesn't work because this is not any kind of listener but just static code, so the only time it's ever getting checked is when the code is first executed. Any subsequent changes to draw.drawing will not affect the button at all. Commented Jan 9, 2016 at 0:39
  • @furas Thanks. It works now. Commented Jan 9, 2016 at 0:41
  • @R.Kap changing the state every time you change the variable is a working option, but probably not a good practice. I would consider using a common pattern like the observer-pattern. Commented Jan 9, 2016 at 0:45
  • 1
    Because it'll get heavier and less portable the more you are using it this way. The observer-pattern (or something similar) is a unified solution for this kind of problem which can also be used at other parts of your program, without having to impelement it multiple times. There is nothing wrong with doing it manually in small scripts, but if you are writing larger software good design becomes inevitable. I just wanted to mention it at least once because I feel that you should at least be informed that there is a possible (but not quite that easy) other solution. Commented Jan 9, 2016 at 0:51

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.