且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法在 Tkinter 中显示图像

更新时间:2023-02-22 15:27:34

好的,我想通了.显然由于python处理垃圾处理的方式,图片只是被删除了.需要在全局范围内引用图像.这是我最终使用的工作代码:

Ok, I figured It out. Apparently due to the way that python deals with garbage disposal the pictures just get erased. A reference to the image in the global scope is required. This is the working code I eventually ended up using:

self.photo = PhotoImage(file="noart.ppm")
    self.Artwork = Label(self.frame, image=self.photo)
    self.Artwork.photo = self.photo
    self.Artwork.pack()

self.Artwork.photo = self.photo 是重要的部分.它确保将显示图像.

that self.Artwork.photo = self.photo is the important part. It ensures that the Image will be shown.