473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 = "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()
Apr 25 '06 #1
6 971
Schüle Daniel wrote:
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
Apr 25 '06 #2
Farshid Lashkari schrieb:
Schüle Daniel wrote:
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.


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

Apr 25 '06 #3
Schüle Daniel wrote:
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
Apr 26 '06 #4
[..]
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
Apr 26 '06 #5
On 2006-04-25, Farshid Lashkari <la********@SPAMworldviz.com> wrote:
Schüle Daniel wrote:
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.


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

http://effbot.org/tkinterbook/bitmapimage.htm

--
Grant Edwards grante Yow! LOU GRANT froze
at my ASSETS!!
visi.com
Apr 26 '06 #6
On 2006-04-25, Schüle Daniel <uv**@rz.uni-karlsruhe.de> wrote:
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.

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.

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

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

Similar topics

11
by: Maciej Nadolski | last post by:
Hi! I can`t understand what php wants from me:( So: Cannot send session cache limiter - headers already sent (output started at /home/krecik/public_html/silnik.php:208) in...
6
by: Ken Varn | last post by:
Sometimes when I try to close my managed C++ application, the following dialog displays in Win 2000 Pro: The title of the dialog is "Server Busy". The message is "This action cannot be completed...
8
by: Peter Foti | last post by:
I'm working on a site and the client said he thinks "the main title should be around 28pt and the subtitle probably 18pt". I know pt's are bad, and I want to convince him that he should NOT be...
1
by: mansa | last post by:
a) A company receives hundreds of orders every week through the mail. Explain carefully how BATCH- PROCESSING might be used. Describe the role of the BATCH TOTAL in this process. Explain how batch...
6
by: Dave Young | last post by:
I'm looking at some code that i've inherited and I'm not really familar with what's going on here and was hoping somone could explain it to me. For reference: f1 is a long f2 is a long ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...

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.