سؤال

Is there something I should know about passing complex expressions as keyword arguments? The example I'm running into is in tkinter:

image = gui.utils.get_image(self.get_icon('pause'), dimensions=(50, 50))
button = ttk.Label(self.mainframe, image=image)
button.place(x=0, y=-10, relwidth=1, relheight=1)

Works, but the following doesn't:

button = ttk.Label(self.mainframe, image=gui.utils.get_image(self.get_icon('pause'), dimensions=(50, 50)))
button.place(x=0, y=-10, relwidth=1, relheight=1)

What's the difference? gui.utils.get_image(self.get_icon('pause'), dimensions=(50, 50)) should be evaluated at the time I pass it to the constructor. Is this a bug in python (I'm on 2.7), or is it somehow tkinter's error? If it helps, I'm running this code in a subthread, but I'm not referencing any objects outside this thread.

Edit:

By "works", I mean the desired image is attached to the background of the label. In the second example, the label appears with the proper size and position, but with a blank background.

هل كانت مفيدة؟

المحلول

You need to keep a reference to the image. Otherwise, as Fredrik Lundh explains,

Tkinter tells Tk to release the image. But since the image is in use by a widget, Tk doesn’t destroy it. Not completely. It just blanks the image, making it completely transparent…

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top