Hello,
I'm a newbie to Python (literally, within the last two weeks), and I am
playing around with Tkinter to build some simple GUIs. I am attempting
to build a simple class that displays a GIF. Here is the code:
#start of code
from Tkinter import *
class DisplayPict(Frame):
def __init__(self,parent=None):
Frame.__init__(self,parent)
self.pack()
self.img=PhotoImage(file="moon.gif")
self.can=Canvas(self)
self.can.create_image(2,2,image=self.img,anchor=NW )
self.can.pack(fill=BOTH)
#keep a reference to the img around
self.can.photo=self.img
if __name__ == '__main__': DisplayPict().mainloop()
#end of code
Anyway, I started with this code in Windoze using IDLE, and everytime I
ran it I kept getting an empty frame (well, presumably with a Canvas
that had nothing in it). I kept thinking that it had to be something
to do with the whole images-get-garbage-collected-if-not-referenced
issue that I read so much about. So I tried a bunch of different
permutations and nothing made a difference.
On a whim, I tried this code under Linux, and amazingly, it worked just
fine: the image was displayed as expected.
So...I am confused. I tried to do my homework (i.e., read through
"Programming Python", "Learning Python", and scouring the internet),
but I can't come up with an explanation as to why this doesn't work
under Windoze. Can anyone out there show me the error in what I have?
Thanks in advance,
John O 4 1792 jo**********@gmail.com wrote: Hello, I'm a newbie to Python (literally, within the last two weeks), and I am playing around with Tkinter to build some simple GUIs. I am attempting to build a simple class that displays a GIF. Here is the code:
#start of code from Tkinter import *
class DisplayPict(Frame):
def __init__(self,parent=None): Frame.__init__(self,parent) self.pack() self.img=PhotoImage(file="moon.gif") self.can=Canvas(self) self.can.create_image(2,2,image=self.img,anchor=NW ) self.can.pack(fill=BOTH)
#keep a reference to the img around self.can.photo=self.img
if __name__ == '__main__': DisplayPict().mainloop() #end of code
Anyway, I started with this code in Windoze using IDLE, and everytime I ran it I kept getting an empty frame (well, presumably with a Canvas that had nothing in it). I kept thinking that it had to be something to do with the whole images-get-garbage-collected-if-not-referenced issue that I read so much about. So I tried a bunch of different permutations and nothing made a difference.
On a whim, I tried this code under Linux, and amazingly, it worked just fine: the image was displayed as expected.
So...I am confused. I tried to do my homework (i.e., read through "Programming Python", "Learning Python", and scouring the internet), but I can't come up with an explanation as to why this doesn't work under Windoze. Can anyone out there show me the error in what I have?
Thanks in advance, John O
I'm inclined to think that its your python installation. It worked for
me with both the cygwin python (both in the console and in an xterm) and
it also worked for me with idle using enthought python. I haven't tried
the active state python.
> I'm inclined to think that its your python installation. It worked for me with both the cygwin python (both in the console and in an xterm) and it also worked for me with idle using enthought python. I haven't tried the active state python.
Thanks for giving it a shot. I just checked the version of Python I
was using: 2.3.4 under Linux, and 2.4.2 under Windoze. So there is a
difference here I guess, though I'd be surprised if it were actually
any kind of "fix" between the two versions.
As a side note, under Win, if I don't put the code in a class it seems
to work fine. In other words:
#start code
from Tkinter import *
win = Tk()
img = PhotoImage(file="moon.gif")
can = Canvas(win)
can.create_image(2,2,image=img, anchor=NW)
#no requirement for keeping an instance of the image around here
win.mainloop()
#end code
....and I get a moon displayed in the top-level window. This got me
wondering if I was misusing the Frame and/or Canvas widget in some
fashion (since obviously there is no Frame in the above snippet).
Any other thoughts out there?
TIA,
John jo**********@gmail.com wrote: I'm inclined to think that its your python installation. It worked for me with both the cygwin python (both in the console and in an xterm) and it also worked for me with idle using enthought python. I haven't tried the active state python.
Thanks for giving it a shot. I just checked the version of Python I was using: 2.3.4 under Linux, and 2.4.2 under Windoze. So there is a difference here I guess, though I'd be surprised if it were actually any kind of "fix" between the two versions.
As a side note, under Win, if I don't put the code in a class it seems to work fine. In other words: #start code from Tkinter import * win = Tk() img = PhotoImage(file="moon.gif") can = Canvas(win) can.create_image(2,2,image=img, anchor=NW)
#no requirement for keeping an instance of the image around here
win.mainloop() #end code
...and I get a moon displayed in the top-level window. This got me wondering if I was misusing the Frame and/or Canvas widget in some fashion (since obviously there is no Frame in the above snippet).
Any other thoughts out there?
TIA, John
There is a difference between the above code and your prior code, namely
in that you have explicitly instantiated Tk and put your canvas into the
"root" toplevel. Try this in idle where it was failing:
#start of code
from Tkinter import *
class DisplayPict(Frame):
def __init__(self,parent=None):
Frame.__init__(self,parent)
self.pack()
self.img=PhotoImage(file="moon.gif")
self.can=Canvas(self)
self.can.create_image(2,2,image=self.img,anchor=NW )
self.can.pack(fill=BOTH)
#keep a reference to the img around
self.can.photo=self.img
if __name__ == '__main__':
tk = Tk()
DisplayPict(tk).mainloop()
#end of code
James There is a difference between the above code and your prior code, namely in that you have explicitly instantiated Tk and put your canvas into the "root" toplevel. Try this in idle where it was failing:
<snip>
Problem solved...I tried James' suggestion (explicitly instantiating
the root Tk window, and then running the mainloop() as suggested), but
it produced the same result i.e. no photo. Ugghhhh...
I then looked REALLY closely at my code and noticed that my __init__
function was actually ___init___ (3 underscores instead of 2). Thus, a
Frame object was being built, but the __init__ function wasn't getting
run. So no image was ever getting added. Silly underscores...or,
silly Python newbie :-) It now works under both Linux and Windoze
just fine.
I would have thought that having no __init__ function would flag an
error, but I guess this isn't necessary. I thought about grabbing PDB
to debug, and I bet this would have shown the error immediately (i.e.,
it would never have called the function). Being a newbie is such a
joy...
Thanks for the help,
John This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: wang xiaoyu |
last post by:
Hello,everyone.
my program runs well in windows,i use tkSimpleDialog to receive some
input,but when i copy my program into Linux RH8.0,entrys in my
tkSimpleDialog derived Dialog have a vital...
|
by: DoubleM |
last post by:
Hi,
I'm running Python2.3.3c1 on Mandrake 9.1
The following code is designed to bring up a window with a button labeled
"popup". Clicking on the popup buttons triggers a secondary window with...
|
by: corrado |
last post by:
Hello
I have an application running several thread to display some financial
data;
basically I have a thread displaying HTML tables by means of Tkhtml,
another implementing a scrolling ticker...
|
by: peter |
last post by:
I've come across a weird difference between the behaviour of the
Tkinter checkbox in Windows and Linux. The issue became apparent in
some code I wrote to display an image in a fixed size canvas...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |