473,714 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(men u=menu)

filemenu = Menu(menu)
menu.add_cascad e(label="File", menu=filemenu)
filemenu.add_co mmand(label="Ne w", command=callbac k('new'))
filemenu.add_co mmand(label="Op en...", command=callbac k('open'))
filemenu.add_se parator()
filemenu.add_co mmand(label="Ex it", command=callbac k('exit'))

helpmenu = Menu(menu)
menu.add_cascad e(label="Help", menu=helpmenu)
helpmenu.add_co mmand(label="Ab out...", command=callbac k('about'))

img00 = PhotoImage(form at='gif',data=
'R0lGODlhKwAQAJ EAACWgA/3bYYNOGgAAACwAA AAAKwAQAAACfIyP qcsrD2M0'
+'oAJqa8h29yAkI TiG3HWmKWiUrdtp seZdtcfmJSyjvf2 Z5Q671u0wA9I+Rt Lj'
+'ZcwgfxglTTchj qS34JUrCUMQySOz ih07Tb62eeneqSf U8vsmf65xZa8S/zI3'
+'dlLD5deRl1dlx dT4MYIA2TBJuSZ2 iZkZVgAAOw==')

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

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

from Tkinter import *

class gifMenu:
def __init__(self, parent):

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

filemenu = Menu(menu)
menu.add_cascad e(label="File", menu=filemenu)
filemenu.add_co mmand(label="Ne w", command=callbac k('new'))
filemenu.add_co mmand(label="Op en...", command=callbac k('open'))
filemenu.add_se parator()
filemenu.add_co mmand(label="Ex it", command=callbac k('exit'))

helpmenu = Menu(menu)
menu.add_cascad e(label="Help", menu=helpmenu)
helpmenu.add_co mmand(label="Ab out...", command=callbac k('about'))

img00 = PhotoImage(form at='gif',data=
'R0lGODlhKwAQAJ EAACWgA/3bYYNOGgAAACwAA AAAKwAQAAACfIyP qcsrD2M0'
+'oAJqa8h29yAkI TiG3HWmKWiUrdtp seZdtcfmJSyjvf2 Z5Q671u0wA9I+Rt Lj'
+'ZcwgfxglTTchj qS34JUrCUMQySOz ih07Tb62eeneqSf U8vsmf65xZa8S/zI3'
+'dlLD5deRl1dlx dT4MYIA2TBJuSZ2 iZkZVgAAOw==')

newLabel = Label(parent,im age=img00)
t = str(img00.width ()) + ' wide x ' + str(img00.heigh t()) + ' high'
newLabel.pack()
Label(parent,te xt='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(titl e)

#exitButton = Tkinter.Button( root, text = 'Exit', command =
root.destroy)
#exitButton.pac k(side = 'bottom')
gifMenu = gifMenu(root)
root.mainloop()
Jul 18 '05 #1
1 6362
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(men u=menu)

filemenu = Menu(menu)
menu.add_cascad e(label="File", menu=filemenu)
filemenu.add_co mmand(label="Ne w", command=callbac k('new'))
filemenu.add_co mmand(label="Op en...", command=callbac k('open'))
filemenu.add_se parator()
filemenu.add_co mmand(label="Ex it", command=callbac k('exit'))

helpmenu = Menu(menu)
menu.add_cascad e(label="Help", menu=helpmenu)
helpmenu.add_co mmand(label="Ab out...", command=callbac k('about'))

img00 = PhotoImage(form at='gif',data=
'R0lGODlhKwAQAJ EAACWgA/3bYYNOGgAAACwAA AAAKwAQAAACfIyP qcsrD2M0'
+'oAJqa8h29yAkI TiG3HWmKWiUrdtp seZdtcfmJSyjvf2 Z5Q671u0wA9I+Rt Lj'
+'ZcwgfxglTTchj qS34JUrCUMQySOz ih07Tb62eeneqSf U8vsmf65xZa8S/zI3'
+'dlLD5deRl1dlx dT4MYIA2TBJuSZ2 iZkZVgAAOw==')

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

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

from Tkinter import *

class gifMenu:
def __init__(self, parent):

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

filemenu = Menu(menu)
menu.add_cascad e(label="File", menu=filemenu)
filemenu.add_co mmand(label="Ne w", command=callbac k('new'))
filemenu.add_co mmand(label="Op en...", command=callbac k('open'))
filemenu.add_se parator()
filemenu.add_co mmand(label="Ex it", command=callbac k('exit'))

helpmenu = Menu(menu)
menu.add_cascad e(label="Help", menu=helpmenu)
helpmenu.add_co mmand(label="Ab out...", command=callbac k('about'))

img00 = PhotoImage(form at='gif',data=
'R0lGODlhKwAQAJ EAACWgA/3bYYNOGgAAACwAA AAAKwAQAAACfIyP qcsrD2M0'
+'oAJqa8h29yAkI TiG3HWmKWiUrdtp seZdtcfmJSyjvf2 Z5Q671u0wA9I+Rt Lj'
+'ZcwgfxglTTchj qS34JUrCUMQySOz ih07Tb62eeneqSf U8vsmf65xZa8S/zI3'
+'dlLD5deRl1dlx dT4MYIA2TBJuSZ2 iZkZVgAAOw==')

newLabel = Label(parent,im age=img00)
t = str(img00.width ()) + ' wide x ' + str(img00.heigh t()) + ' high'
newLabel.pack()
Label(parent,te xt='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(titl e)

#exitButton = Tkinter.Button( root, text = 'Exit', command =
root.destroy)
#exitButton.pac k(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
12566
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 greatly appreciated. -- here is snip of working code:
3
2340
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, QuickTimeMovie, that would wrap up the QuickTimeTcl Tcl extension as a Python class. All seems to work pretty fine until the Tkinter application is closed, when the Python interpreter crashes with an error of the following kind: The instruction at...
1
6248
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 doesn't fit with my appl which is designed primarily for running on Windows and OS X. The default button visual that appears is basically a small rectangle drawn in relief inside a larger one. I'd like to replace this with a downward pointing...
7
4537
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, Harlin
3
8777
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 being processed. I can't get the image to update, though. I trimmed it down to a basic app indicating the problem and the code is at the end of this message. It should display the three listed sample images, one after another. The thing is,...
2
3735
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. canvas=Canvas(master,blah...) canvas.pack() def change_pic(path): global pic image=Image() #I'm using PIL to use the images, but I
3
2356
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 different pages. All that's working fine... The problem I'm having is that the images don't show onscreen the first time the "first page" of results shows up. When I click the "search again" button, and have more than the original results page to...
4
6374
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, None) Label(self, text="Hello").pack() Button(self, text="OK", command=self.ok).pack()
2
4125
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. I have two functions. Here are the relevant excerpts: def makeGUI(): ###lots of code to set up toplevel windows
0
9309
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9171
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9010
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7946
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6629
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4462
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4717
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2107
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.