本文介绍了Tkinter 标签不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习一些 tkinter.我无法让 tkinter 显示图标.我不知道哪里出错了.它不会产生任何错误并且它尊重图像的大小,但它是不可见的.我在网上找到的所有内容都告诉添加第二个引用以防止来自 python 的垃圾收集,但不知何故这并不是诀窍.这是我的代码出错的部分:

I'm trying to learn some tkinter. I can't get tkinter to display an icon. I don't know where it goes wrong. It does not produce any error and it respects the size of the image, but it's invisible. Everything I found online tells to add a second reference to prevent garbage collection from python, but somehow this did not the trick.Here is the part of my code that goes wrong:

from Tkinter import *
from PIL import Image, ImageTk

class GUI:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        #status bar
        self.bar = Frame(root, relief=RIDGE, borderwidth=5)
        self.bar.pack(side=TOP)

        self.iconPath = 'data/icons/size.png'
        self.icon = ImageTk.PhotoImage(Image.open(self.iconPath))
        self.icon_size = Label(self.bar, image = self.icon)
        self.icon_size.pack(side=LEFT)

root = Tk()


app = GUI(root)

root.mainloop()

推荐答案

当您将 PhotoImage 或其他 Image 对象添加到 Tkinter 小部件时,您必须保留自己对图像对象的引用.如果不这样做,图像将不会始终显示.

When you add a PhotoImage or other Image object to a Tkinter widget, you must keep your own reference to the image object. If you don’t, the image won’t always show up.

决定这里

这篇关于Tkinter 标签不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 14:35