How to make an if statement using widget option values as conditions? How to make an if statement using widget option values as conditions? tkinter tkinter

How to make an if statement using widget option values as conditions?


First, never use the expression pattern something=something else == True!

Second, take a look at this related (but not duplicate) question.

As you can see there, the cget method will return the current value for an option. As this manual page mentions, cget is analogous to widget["option"].


So, to answer your question directly, the if condition you need would be along the lines of:

if button['image']==photo2:   anotherFunction()


I'm new here and couldn't comment. I hope I'm not flaunting SO policy by resorting to answering instead.

@Tersosauros

"First, never use the expression pattern something=something else == True!"

Where do you see this pattern and why should it be avoided? What could replace it? (I know you're a tersosaurus but "never use X" just seems TOO terse and uninformative).

@Arwan Credoz I know you got your answer but... If you simply want to check whether the value of "turn" is a an even/odd number and is within a given range, use a bounds check followed by modulus instead (maybe this is what @Tersosauros was hinting at?). Also, the value of "turn" will always be incremented if it's within range(0,10) so there's no need to write "turn+=1" twice. If I've understood your intentions correctly, you could probably rewrite "functionPlus" to something like this and add Tersosaurus' addition where appropriate:

def functionPlus(button):    global turn    if 0 < turn < 10:        if turn % 2 == 0:            button.config(image=photo3,width="100",height="100")        else:            button.config(image=photo2,width="100",height="100")        turn += 1