Image loading problem

Christian Gollwitzer auriocus at gmx.de
Sun May 22 16:57:46 EDT 2016


Am 22.05.16 um 22:19 schrieb Random832:
> On Sun, May 22, 2016, at 15:37, Michael Torrie wrote:
>> The reference is indeed held by the label but the problem is the label
>> is a Tcl/Tk object, thinly wrapped in Python.
>
> Okay but then in that case why doesn't the image get instantiated as a
> Tcl/Tk object which the label holds a reference to and then nobody cares
> if the python image object gets collected?

Actually, I think it could be solved, and not too complicated either. In 
Tk, objects are deleted explicitly. This is how you'd do it natively:

	image create photo bla -file myicon.png
	# create an image called "bla"
	pack [label .l -image bla]
	# display it in a label with name .l

Of course, the label references the image object. For instance, if you 
read in new image data

	bla read newicon.png -shrink

then the label will update.

What happens, when you create all those things from Tkinter, the Label 
object deletes the image in it's destructor:

	image delete bla # this will turn the label blank

This is IMHO correct, but when you add an image to the label through 
Tkinter, then it only does it through Tk (i.e. the second line in the 
above code). It should store a reference the Python image object inside 
the label object. This is akin to a "dangling pointer" in C. It would 
just be some work to replicate this for all widgets in Tk, there are 
buttons etc. but in essence I think it would work.

	Christian



More information about the Python-list mailing list