I moved in with my girlfriend to our new apartment in Karlskrona, KNA, last weekend and since then we have generally been busy with getting the place in order and exploring KNA.
I did however finish one more assignment in the “Multimedia programming with Python”. Which sounds much more fun than it actually is. The course recommends the usage of Tkinter, which I find to be extremely boring and feels very old and poorly designed. Not to mention some problems it seems to have with Python’s garbage collector.
With the assignment I submitted yesterday I chose to declare a function that would load some images and display them on the screen, but for some unexplainable reason the images just wouldn’t show on the screen although the window would get the correct size to fit every image.
So after an hour of two of debugging and googling I found the problem with the following snippet of code:
image = Image.open("python.jpg")This code is completely appropriate and loads an image from file and works in all cases, except when it is used within a function.
tkpicture = ImageTk.PhotoImage(image)
label_image = Label(self.parent, image=tkpicture)
Apparently the line Label(self.parent, image=tkpicture) does not trigger python to increment the reference count of tkpicture. So when one returns from the loadImage() function the garbage collector thinks tkpicture is unused and therefore deletes it.
The “solution” to this problem is to do the following:
label_image = Label(self.parent, image=tkpicture)This seems infinitely inane since assigning tkpicture to image is what the Label constructor is doing already. I highly doubt that there isn’t a better way of doing this. Not to mention that doing this prevents Python from cleaning up tkpicture by itself. So one have to make sure to do this:
label_image.image = tkpicture
label_image.image = oOtherwise one would be leaking memory due to that extra reference.
Just one final assignment to be done and then I am done with this course, and a happy day it will be.
Going to start reading the iPhone book soon. That I can guarantee will be a much more pleasant experience.
cozy little blog u got here. keep writing henke , so i can keep track of you
ReplyDelete