在Tkinter中,我们有时必须创建对 转换后的图像(例如),以便在超出范围时不会被销毁。你知道吗
一种常见的方法是将变量添加到其小部件实例中。例如:
bard = Image.open("bardejov.jpg")
bardejov = ImageTk.PhotoImage(bard)
label1 = Label(self, image=bardejov)
label1.image = bardejov #<<<<<<<<<<<<<<<<<<<<<
label1.place(x=20, y=20)
这是Zetcode的Jan Bodnar发布的一个示例的一部分,我标记了这个示例。bardejov是函数中的一个局部变量,如果注释掉标记的行,就不会得到图像,因为当函数返回时图像会被破坏,标签只会看到“none”。你知道吗
我是Tkinter的新手,这让我很担心,在系统代码中添加新属性,有人建议:
class S():
# To make an object 'accessible', and thus save
# it from garbage collection.
fred = []
@classmethod
def save(cls, x):
cls.fred.append(x)
这在简的例子中确实奏效了:
bard = Image.open("xxxx.jpg")
bardejov = ImageTk.PhotoImage(bard)
label1 = Label(self, image=bardejov)
#label1.image = bardejov
S.save(bardejov)
但是还好吗?有什么不良反应吗?你知道吗
这不是问题,不管好坏,你都必须去做。
你可以定义自己的
PhotoImageLabel
,而不是一遍又一遍地做。你知道吗例如:
这不太可能,因为
ImageTk.PhotoImage
不是tkinter
的一部分。 通过将图像对象绑定到小部件,可以扩展PhotoImage
,使其行为类似于tkinter
对象:例如:
相关问题 更多 >
编程相关推荐