can someone explain why ..

  • Thread starter =?ISO-8859-1?Q?Sch=FCle_Daniel?=
  • Start date
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

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 = "Zeitrechner")
root.config(borderwidth = 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(width = image.width(), height = image.height())
root.maxsize(width = 2*image.width(), height = image.height())

imageLabel.pack()
frame1.pack(side = LEFT)

def mainloop(self):
root.mainloop()

main = Main()
main.mainloop()
 
F

Farshid Lashkari

Schüle Daniel said:
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


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
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

Farshid said:
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.

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
 
F

Farshid Lashkari

Schüle Daniel said:
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

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
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

[..]
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?

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

frame1 = Frame(master = root)
frame1.pack(side = 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
 
G

Grant Edwards

Correct.

image is local variable of imageLabel

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

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top