By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,526 Members | 1,618 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,526 IT Pros & Developers. It's quick & easy.

Confusing: Canvas image working in function but not in class

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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 )

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.