简体   繁体   English

python kwargs表达评估

[英]python kwargs expression evaluation

Is there something I should know about passing complex expressions as keyword arguments? 关于将复杂表达式作为关键字参数传递,我应该知道些什么吗? The example I'm running into is in tkinter: 我遇到的例子是在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. gui.utils.get_image(self.get_icon('pause'), dimensions=(50, 50))应该在我传递给构造函数时进行评估。 Is this a bug in python (I'm on 2.7), or is it somehow tkinter's error? 这是python中的一个错误(我在2.7),还是以某种方式tkinter的错误? 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 , 否则,正如Fredrik Lundh解释的那样

Tkinter tells Tk to release the image. Tkinter告诉Tk发布图像。 But since the image is in use by a widget, Tk doesn't destroy it. 但由于图像由小部件使用,因此Tk不会破坏它。 Not completely. 不完全的。 It just blanks the image, making it completely transparent… 它只是使图像空白,使其完全透明......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM