473,782 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple Tkinter app works in Linux, not in Windows

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(Fra me):

def __init__(self,p arent=None):
Frame.__init__( self,parent)
self.pack()
self.img=PhotoI mage(file="moon .gif")
self.can=Canvas (self)
self.can.create _image(2,2,imag e=self.img,anch or=NW)
self.can.pack(f ill=BOTH)

#keep a reference to the img around
self.can.photo= self.img

if __name__ == '__main__': DisplayPict().m ainloop()
#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
"Programmin g 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

Feb 10 '06 #1
4 1807
jo**********@gm ail.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(Fra me):

def __init__(self,p arent=None):
Frame.__init__( self,parent)
self.pack()
self.img=PhotoI mage(file="moon .gif")
self.can=Canvas (self)
self.can.create _image(2,2,imag e=self.img,anch or=NW)
self.can.pack(f ill=BOTH)

#keep a reference to the img around
self.can.photo= self.img

if __name__ == '__main__': DisplayPict().m ainloop()
#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
"Programmin g 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.
Feb 10 '06 #2
> 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_imag e(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

Feb 10 '06 #3
jo**********@gm ail.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_imag e(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(Fra me):

def __init__(self,p arent=None):
Frame.__init__( self,parent)
self.pack()
self.img=PhotoI mage(file="moon .gif")
self.can=Canvas (self)
self.can.create _image(2,2,imag e=self.img,anch or=NW)
self.can.pack(f ill=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
Feb 10 '06 #4
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...o r,
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

Feb 10 '06 #5

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

Similar topics

0
3603
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 problem:only one entry can receive key event,'tab' key to navigate between entrys is not valid too,when i use mouse to focus a entry(which can not navigate through 'tag' key),no matter what key i pressed the entry receive no reply.But in window they...
3
3344
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 a button labeled "ok". The second window is supposed to be modal - it should not be possible to reset the focus back to the first window or close the first window without first closing the second. The program works just fine in Windows XP...
1
3860
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 based on a Text widget with embedded windows and a thread running the Tkinter mainloop plus several other thread dealing with the scheduling of the contents and the acquisition of data but not using graphic widgets. I run the same code on Linux...
4
3050
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 widget. If a checkbox was set then the image should be shrunk as necessary to fit the canvas while if cleared it should appear full size with scrollbars if necessary. The code worked fine under Linux (where it was developed). But under Windows,...
0
9639
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
9479
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
10311
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
10146
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
8967
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...
0
5378
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
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
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.