473,657 Members | 2,358 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 6342
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
12537
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
2332
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
6247
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
4535
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
8756
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
3732
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
2353
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
6348
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
4116
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
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
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...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8613
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
7351
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
6176
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
4173
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...
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.