473,249 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,249 software developers and data experts.

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

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'
+'oAJqa8h29yAkITiG3HWmKWiUrdtpseZdtcfmJSyjvf2Z5Q67 1u0wA9I+RtLj'
+'ZcwgfxglTTchjqS34JUrCUMQySOzih07Tb62eeneqSfU8vsm f65xZa8S/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'
+'oAJqa8h29yAkITiG3HWmKWiUrdtpseZdtcfmJSyjvf2Z5Q67 1u0wA9I+RtLj'
+'ZcwgfxglTTchjqS34JUrCUMQySOzih07Tb62eeneqSfU8vsm f65xZa8S/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()
Jul 18 '05 #1
1 6214
midtoad wrote:
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'
+'oAJqa8h29yAkITiG3HWmKWiUrdtpseZdtcfmJSyjvf2Z5Q67 1u0wA9I+RtLj'
+'ZcwgfxglTTchjqS34JUrCUMQySOzih07Tb62eeneqSfU8vsm f65xZa8S/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'
+'oAJqa8h29yAkITiG3HWmKWiUrdtpseZdtcfmJSyjvf2Z5Q67 1u0wA9I+RtLj'
+'ZcwgfxglTTchjqS34JUrCUMQySOzih07Tb62eeneqSfU8vsm f65xZa8S/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
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Adonis | last post by:
I wish to manually move widgets in Tkinter, now I have successfully done it, but with odd results, I would like to move the widgets with a much smoother manner, and better precision. Any help is...
3
by: Mickel Grönroos | last post by:
Hi everybody, I'm using QuickTimeTcl (3.1) to be able to play movie files in my Tkinter application (Python 2.3.2) on Windows 2000. I was planning to write a simple wrapper class,...
1
by: Martin | last post by:
Anybody know how to change the menu button that is displayed by the Tkinter Menubutton and the (derived from it) OptionMenu classes? I think it must be something from the Motif look and feel which...
7
by: Harlin Seritt | last post by:
I was looking at the Tcl/Tk sourceforge page and found that there were a couple of new widgets being produced for Tcl 8.5. Does anyone know if there are any Tkinter wrappers somewhere? thanks, ...
3
by: Terry Carroll | last post by:
I've got a small batch image-processing program (it adds the time a digital photo was taken to the lower right of the image), and as a feature, I wanted to show a thumbnail of each image it was...
2
by: Tuvas | last post by:
I've been trying to use a canvas to display different pictures on a Tkinter interface. However, it doesn't seem to update the information. Ei, I have something like this. ...
3
by: aldonnelley | last post by:
Hi all. Just having a weird problem with tkinter. I'm trying to make a gui that shows results from an image search, with a "forward" and "back" button so the user can compare results from...
4
by: Claus Tondering | last post by:
I am trying to make a Tkinter main window appear and disappear, but I have problems with that. Here is a small code sample: class MyDialog(Frame): def __init__(self): Frame.__init__(self,...
2
by: Kevin Walzer | last post by:
I'm trying to construct a simple Tkinter GUI and I'm having trouble with getting the value of an entry widget and passing it onto a callback function. But I'm not grokking variable scope correctly....
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.