Simple Tkinter app works in Linux, not in Windows

James Stroud jstroud at ucla.edu
Fri Feb 10 00:08:50 EST 2006


john.orlando at 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



More information about the Python-list mailing list