Confusing: Canvas image working in function but not in class

M

Mudcat

I have an image that displays on a canvas that works unless I put the
same code in a class. I can't figure that out. Here's what works:

def uts5100(self):
self.screen = Toplevel( self.master )
self.screen.geometry("+100+50")
self.screen.grab_set()
self.screen.focus_set()

self.screenType = "uts5100"

self.canvas = Canvas(self.screen)

self.canvas.create_image(20, 20, image = self.empty5100,
anchor=NW, tags = "chasis" )

self.canvas.pack()

The image is opened in another function:

def openImages(self):
self.empty5100 = ImageTk.PhotoImage( Image.open("Images/" +
"5100empty.bmp") )


However if I do the same thing, but in it's own class, the image does
not appear:

def uts5100(self):
sys5100 = UTS5100( self.master )

class UTS5100:
def __init__(self, master):
self.master = master
self.openImages()
self.draw()

def openImages(self):
self.empty5100 = ImageTk.PhotoImage( Image.open("Images/" +
"5100empty.bmp") )


def draw(self):
self.screen = Toplevel( self.master )
self.screen.geometry("+100+50")
self.screen.grab_set()
self.screen.focus_set()

self.screenType = "uts5100"

self.canvas = Canvas(self.screen)

self.image = self.canvas.create_image(20, 20, image =
self.empty5100, anchor=NW, tags = "chasis" )

self.canvas.pack()


They are called from a button that when pressed creates the window
where the image is supposed to appear. Obviously there is something
that I am missing when putting this window into it's own class, but I
can't figure it out.

Anyone got an idea why this is happening?

Thanks
 
M

Mudcat

I will answer my own question in case anyone else ever has this
problem.

I knew the problem (although some say it's not) existed with the
namespace of pictures, that if you didn't save the pictures in
persistent memory they would disappear as soon as the function that
called them was exited. So a while ago I went through this and fixed it
by saving all my pictures and widgets that held them as class objects.

However, apparently I was causing the same thing to happen by not
saving the class instance as an object of the class that called it. So
this fixed it:

def uts5100(self):
self.sys5100 = UTS5100( self.master )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top