【问题标题】:Display .gif image using tkinter, ttk and python3使用 tkinter、ttk 和 python3 显示 .gif 图像
【发布时间】:2015-12-11 22:45:05
【问题描述】:

我正在尝试显示图像。

from tkinter import *
from tkinter import ttk

root = Tk()
myimage = PhotoImage(file='world.gif')
ttk.Label(root, image=myimage)
root.mainloop()

当我运行它时,我希望看到我的图像。但我只看到一个纯灰色图像。 任何帮助表示赞赏。

【问题讨论】:

    标签: image python-3.x tkinter ttk


    【解决方案1】:

    您需要保留对 Tkinter 对象的引用。像这样的东西(我这里没有使用 ttk):

    myimage = PhotoImage(file='world.gif')
    
    label = Label(image=myimage)
    label.image = myimage # the reference
    label.pack()
    

    垃圾收集器正在尝试丢弃 Tkinter 图像对象,因为 Tkinter 没有保留对它的引用。所以 Tkinter 释放图像,Tk 小部件没有,你得到一个奇怪的空白图像。

    【讨论】:

    • 非常感谢亚当。我必须在变量中引用 ttk.Label:label = ttk.Label(root,image=myimage),然后使用打包方法 label.pack()
    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多