can someone explain why ..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Schüle Daniel

    #1

    can someone explain why ..

    I don't understand what is the difference between commented lines
    1 and 2

    with 1 uncommented and 2 commented it works as expected
    with 1 commented and 2 uncommented the picture doesn't appear

    here is my code

    #!/usr/bin/env python

    from Tkinter import *
    from Tkconstants import *

    root = None

    class Main:
    def __init__(self):
    global root
    root = Tk(className = "Zeitrechne r")
    root.config(bor derwidth = 5, relief = GROOVE)
    root.geometry(" 0x0+100+50")

    #self.im = image = PhotoImage(file = "./flower1.gif") #1
    image = PhotoImage(file = "./flower1.gif") #2
    frame1 = Frame(master = root, borderwidth = 3, relief = SUNKEN)
    imageLabel = Label(master = frame1, image = image)

    root.minsize(wi dth = image.width(), height = image.height())
    root.maxsize(wi dth = 2*image.width() , height = image.height())

    imageLabel.pack ()
    frame1.pack(sid e = LEFT)

    def mainloop(self):
    root.mainloop()

    main = Main()
    main.mainloop()
  • Farshid Lashkari

    #2
    Re: can someone explain why ..

    Schüle Daniel wrote:[color=blue]
    > I don't understand what is the difference between commented lines
    > 1 and 2
    >
    > with 1 uncommented and 2 commented it works as expected
    > with 1 commented and 2 uncommented the picture doesn't appear[/color]


    I'm not familiar with Tkinter, but it seems as thought with 2, the
    "image" variable is garbage collected after the constructor of Main is
    called. With 1, you save a reference to the image, so it does not get
    garbage collected.

    -Farshid

    Comment

    • Schüle Daniel

      #3
      Re: can someone explain why ..

      Farshid Lashkari schrieb:[color=blue]
      > Schüle Daniel wrote:[color=green]
      >> I don't understand what is the difference between commented lines
      >> 1 and 2
      >>
      >> with 1 uncommented and 2 commented it works as expected
      >> with 1 commented and 2 uncommented the picture doesn't appear[/color]
      >
      >
      > I'm not familiar with Tkinter, but it seems as thought with 2, the
      > "image" variable is garbage collected after the constructor of Main is
      > called. With 1, you save a reference to the image, so it does not get
      > garbage collected.[/color]

      thx for quick reply :)

      image is local variable of imageLabel
      I would expect that in case imageLabel lives, it should
      hold alife objects bound to its local variables

      I am just curious *why* reference to image is not hold by imageLabel
      which on his part is hold by frame1 .. which is hold by global root

      Regards, Daniel

      Comment

      • Farshid Lashkari

        #4
        Re: can someone explain why ..

        Schüle Daniel wrote:[color=blue]
        > thx for quick reply :)
        >
        > image is local variable of imageLabel
        > I would expect that in case imageLabel lives, it should
        > hold alife objects bound to its local variables
        >
        > I am just curious *why* reference to image is not hold by imageLabel
        > which on his part is hold by frame1 .. which is hold by global root[/color]

        These are the only lines of code that reference "imageLabel ":

        imageLabel = Label(master = frame1, image = image)
        imageLabel.pack ()


        Unless the constructor of Label adds a reference of itself to frame1,
        imageLabel will also become garbage collected at the end of the
        constructor. Are you sure this is the case?

        -Farshid

        Comment

        • Schüle Daniel

          #5
          Re: can someone explain why ..

          [..]
          [color=blue]
          > These are the only lines of code that reference "imageLabel ":
          >
          > imageLabel = Label(master = frame1, image = image)
          > imageLabel.pack ()
          >
          >
          > Unless the constructor of Label adds a reference of itself to frame1,
          > imageLabel will also become garbage collected at the end of the
          > constructor. Are you sure this is the case?[/color]

          yes, now i see it
          even can frame1 be dead after __init__
          it binds itself too root with

          frame1 = Frame(master = root)
          frame1.pack(sid e = LEFT)

          frame1.pack may append frame1 to root's list of all
          widgets, we cannot see it, but it also may not do it

          Thx

          Comment

          • Grant Edwards

            #6
            Re: can someone explain why ..

            On 2006-04-25, Farshid Lashkari <lashkariNO@SPA Mworldviz.com> wrote:[color=blue]
            > Schüle Daniel wrote:[color=green]
            >> I don't understand what is the difference between commented lines
            >> 1 and 2
            >>
            >> with 1 uncommented and 2 commented it works as expected
            >> with 1 commented and 2 uncommented the picture doesn't appear[/color]
            >
            >
            > I'm not familiar with Tkinter, but it seems as thought with 2, the
            > "image" variable is garbage collected after the constructor of Main is
            > called. With 1, you save a reference to the image, so it does not get
            > garbage collected.[/color]

            Yes. That's exactly correct. It's sort of a FAQ. See the
            highlighted note at the bottom of



            --
            Grant Edwards grante Yow! LOU GRANT froze
            at my ASSETS!!
            visi.com

            Comment

            • Grant Edwards

              #7
              Re: can someone explain why ..

              On 2006-04-25, Schüle Daniel <uval@rz.uni-karlsruhe.de> wrote:
              [color=blue][color=green]
              >> I'm not familiar with Tkinter, but it seems as thought with 2,
              >> the "image" variable is garbage collected after the
              >> constructor of Main is called. With 1, you save a reference to
              >> the image, so it does not get garbage collected.[/color][/color]

              Correct.
              [color=blue]
              > image is local variable of imageLabel[/color]

              Actually it's a parameter to the constructor that was called to
              create the "imageLabel " object.
              [color=blue]
              > I would expect that in case imageLabel lives, it should hold
              > alife objects bound to its local variables[/color]

              It would. Therefore the "image" object apparently isn't bound
              to a local variable in the object imageLabel.
              [color=blue]
              > I am just curious *why* reference to image is not hold by
              > imageLabel which on his part is hold by frame1...[/color]

              Good question. Untold millions of programmers have tripped
              over that bug. Well, would you believe untold dozens? I know
              I did. It's a common enough problem that it's given special
              mention in a couple books on Tkinter.

              My _guess_ is that this is one of the misfeatures resulting
              from the way Tkinter is implimented: it's not a real binding of
              Python to the tk library. It's a Python wrapper around a TCL
              interpreter which is bound to the tk library (or something like
              that). This results in some counter-intuitive (non-pythonic)
              behaviors like the one you discovered with bitmap images and
              labels.

              --
              Grant Edwards grante Yow! I feel like I'm
              at in a Toilet Bowl with a
              visi.com thumbtack in my forehead!!

              Comment

              Working...