Python Tkinter comparing PhotoImage objects

Posted by Kyle Schmidt on Stack Overflow See other posts from Stack Overflow or by Kyle Schmidt
Published on 2010-04-16T13:06:41Z Indexed on 2010/04/16 13:23 UTC
Read the original article Hit count: 264

Filed under:
|

In a simple LightsOut game, when I click on a light I need to toggle the image on a button. I'm doing this with Tkinter, so I thought I'd just check and see what image is currently on the button (either 'on.gif' or 'off.gif') and set it to the other one, like this:

def click(self,x,y):
    if self.buttons[x][y].image == self.on:
        self.buttons[x][y].config(image=self.off)
        self.buttons[x][y].image == self.off
    else:
        self.buttons[x][y].config(image=self.on)
        self.buttons[x][y].image == self.on

This ends up always being True - I can turn a lgiht off, but never turn it back on. Did some research, realized that I should probably be using cmp:

def click(self,x,y):
    if cmp(self.buttons[x][y].image,self.on) == 0:
        self.buttons[x][y].config(image=self.off)
        self.buttons[x][y].image == self.off
    else:
        self.buttons[x][y].config(image=self.on)
        self.buttons[x][y].image == self.on

But that gave me the exact same result. Both self.on and self.off are PhotoImage objects. Aside from keeping a separate set of lists which tracks what type of light is in each position and redrawing them every click, is there a way to directly compare two PhotoImage objects like this?

© Stack Overflow or respective owner

Related posts about python

Related posts about tkinter