Simple Tkinter app works in Linux, not in Windows

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • john.orlando@gmail.com

    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

  • James Stroud

    #2
    Re: Simple Tkinter app works in Linux, not in Windows

    john.orlando@gm ail.com wrote:[color=blue]
    > 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
    >[/color]

    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.

    Comment

    • john.orlando@gmail.com

      #3
      Re: Simple Tkinter app works in Linux, not in Windows

      > I'm inclined to think that its your python installation. It worked for[color=blue]
      > 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.[/color]

      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

      Comment

      • James Stroud

        #4
        Re: Simple Tkinter app works in Linux, not in Windows

        john.orlando@gm ail.com wrote:[color=blue][color=green]
        >>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.[/color]
        >
        >
        > 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
        >[/color]

        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

        Comment

        • john.orlando@gmail.com

          #5
          Re: Simple Tkinter app works in Linux, not in Windows

          [color=blue]
          > 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:[/color]

          <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

          Comment

          Working...