Why does my Tkinter GIF image disappear when inside a class?

M

midtoad

I'm trying to display a GIF image in a label as the central area to a
Tkinter GUI. The image does not appear, though a space is made for it.
Why is this so?

I notice that I can display a GIF image in the central area of a simple
menu-bar app as shown below in the first code sample. But, when I set up my
app with a class, as shown below in the second code sample, the image
disappears.

How can I correct this? I'm sure the answer would jump out at me if I
thought more clearly about it...

thanks
S

---
#basicmenu.py : displays GIF image


from Tkinter import *


def callback(text):
print "called the callback!: "+text

root = Tk()

# create a menu
menu = Menu(root)
root.config(menu=menu)

filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=callback('new'))
filemenu.add_command(label="Open...", command=callback('open'))
filemenu.add_separator()
filemenu.add_command(label="Exit", command=callback('exit'))

helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=callback('about'))

img00 = PhotoImage(format='gif',data=
'R0lGODlhKwAQAJEAACWgA/3bYYNOGgAAACwAAAAAKwAQAAACfIyPqcsrD2M0'
+'oAJqa8h29yAkITiG3HWmKWiUrdtpseZdtcfmJSyjvf2Z5Q671u0wA9I+RtLj'
+'ZcwgfxglTTchjqS34JUrCUMQySOzih07Tb62eeneqSfU8vsmf65xZa8S/zI3'
+'dlLD5deRl1dlxdT4MYIA2TBJuSZ2iZkZVgAAOw==')

newLabel = Label(root,image=img00)
t = str(img00.width()) + ' wide x ' + str(img00.height()) + ' high'
newLabel.pack()
Label(root,text='The image is\n'+t).pack()

mainloop()
---
#basicmenu2gifobj.py : doesn't display image

from Tkinter import *

class gifMenu:
def __init__(self, parent):

# create a menu
menu = Menu(parent)
parent.config(menu=menu)

filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=callback('new'))
filemenu.add_command(label="Open...", command=callback('open'))
filemenu.add_separator()
filemenu.add_command(label="Exit", command=callback('exit'))

helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=callback('about'))

img00 = PhotoImage(format='gif',data=
'R0lGODlhKwAQAJEAACWgA/3bYYNOGgAAACwAAAAAKwAQAAACfIyPqcsrD2M0'
+'oAJqa8h29yAkITiG3HWmKWiUrdtpseZdtcfmJSyjvf2Z5Q671u0wA9I+RtLj'
+'ZcwgfxglTTchjqS34JUrCUMQySOzih07Tb62eeneqSfU8vsmf65xZa8S/zI3'
+'dlLD5deRl1dlxdT4MYIA2TBJuSZ2iZkZVgAAOw==')

newLabel = Label(parent,image=img00)
t = str(img00.width()) + ' wide x ' + str(img00.height()) + ' high'
newLabel.pack()
Label(parent,text='The image is\n'+t).pack()

class callback:
def __init__(self, text):
self.text = text
def __call__(self):
print 'Command is: ',self.text

######################################################################

# Create GUI in root window for testing.
if __name__ == '__main__':
title='Menu test with GIF'
root = Tk()
#Pmw.initialise(root)
root.title(title)

#exitButton = Tkinter.Button(root, text = 'Exit', command =
root.destroy)
#exitButton.pack(side = 'bottom')
gifMenu = gifMenu(root)


root.mainloop()
 
G

Greg Krohn

midtoad said:
I'm trying to display a GIF image in a label as the central area to a
Tkinter GUI. The image does not appear, though a space is made for it.
Why is this so?

I notice that I can display a GIF image in the central area of a simple
menu-bar app as shown below in the first code sample. But, when I set up my
app with a class, as shown below in the second code sample, the image
disappears.

How can I correct this? I'm sure the answer would jump out at me if I
thought more clearly about it...

thanks
S

---
#basicmenu.py : displays GIF image


from Tkinter import *


def callback(text):
print "called the callback!: "+text

root = Tk()

# create a menu
menu = Menu(root)
root.config(menu=menu)

filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=callback('new'))
filemenu.add_command(label="Open...", command=callback('open'))
filemenu.add_separator()
filemenu.add_command(label="Exit", command=callback('exit'))

helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=callback('about'))

img00 = PhotoImage(format='gif',data=
'R0lGODlhKwAQAJEAACWgA/3bYYNOGgAAACwAAAAAKwAQAAACfIyPqcsrD2M0'
+'oAJqa8h29yAkITiG3HWmKWiUrdtpseZdtcfmJSyjvf2Z5Q671u0wA9I+RtLj'
+'ZcwgfxglTTchjqS34JUrCUMQySOzih07Tb62eeneqSfU8vsmf65xZa8S/zI3'
+'dlLD5deRl1dlxdT4MYIA2TBJuSZ2iZkZVgAAOw==')

newLabel = Label(root,image=img00)
t = str(img00.width()) + ' wide x ' + str(img00.height()) + ' high'
newLabel.pack()
Label(root,text='The image is\n'+t).pack()

mainloop()
---
#basicmenu2gifobj.py : doesn't display image

from Tkinter import *

class gifMenu:
def __init__(self, parent):

# create a menu
menu = Menu(parent)
parent.config(menu=menu)

filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=callback('new'))
filemenu.add_command(label="Open...", command=callback('open'))
filemenu.add_separator()
filemenu.add_command(label="Exit", command=callback('exit'))

helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=callback('about'))

img00 = PhotoImage(format='gif',data=
'R0lGODlhKwAQAJEAACWgA/3bYYNOGgAAACwAAAAAKwAQAAACfIyPqcsrD2M0'
+'oAJqa8h29yAkITiG3HWmKWiUrdtpseZdtcfmJSyjvf2Z5Q671u0wA9I+RtLj'
+'ZcwgfxglTTchjqS34JUrCUMQySOzih07Tb62eeneqSfU8vsmf65xZa8S/zI3'
+'dlLD5deRl1dlxdT4MYIA2TBJuSZ2iZkZVgAAOw==')

newLabel = Label(parent,image=img00)
t = str(img00.width()) + ' wide x ' + str(img00.height()) + ' high'
newLabel.pack()
Label(parent,text='The image is\n'+t).pack()

class callback:
def __init__(self, text):
self.text = text
def __call__(self):
print 'Command is: ',self.text

######################################################################

# Create GUI in root window for testing.
if __name__ == '__main__':
title='Menu test with GIF'
root = Tk()
#Pmw.initialise(root)
root.title(title)

#exitButton = Tkinter.Button(root, text = 'Exit', command =
root.destroy)
#exitButton.pack(side = 'bottom')
gifMenu = gifMenu(root)


root.mainloop()

Short answer:
Replace img00 with self.img00

Longer answer:
It's a wart (as far as I'm concerned) in Tkinter. As soon as code
execution falls out of __init__ (where img00 was created), the variable
img00 is destroyed, hence Tkinter can't find the image. So you need to
find some way of keeping the image persistant. Like making it an
instance variable, like above, or making it global, etc.

greg
 

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

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top