【问题标题】:Image not displaying in Tkinter Label widget图像未显示在 Tkinter 标签小部件中
【发布时间】:2016-05-11 23:32:42
【问题描述】:

我正在尝试让图像显示在 Tkinter 标签小部件中。此代码在 PyCharm 的一个类中工作,但不会超过主应用程序中的“tk.Label”行。我在这里咨询了其他答案,但无法弄清楚为什么图像没有显示在主应用程序中。

logo_filepath = "/Users/xxx/MASTER/pymol/Master/cache/logos/tmpbhWv2Ts.gif"
self.img = tk.PhotoImage(file = logo_filepath)
self.logo = tk.Label(self, image=self.img)
self.logo.photo = self.img
self.logo.grid(row=0, column=3, rowspan=10, columnspan=4)

【问题讨论】:

  • 为什么它不“越过”那条线?你的电脑崩溃了吗?
  • 您能否提供一个更完整的问题示例?类似于mcve

标签: python tkinter


【解决方案1】:

这是一个非常简单的错误。只要确保您没有在课程之外定义self.[Your Variable]。因为self 仅在类中可用。另外,这是我的代码:

import Tkinter as tk

root = tk.Tk()

logo_filepath = "Your File Path..."
img = tk.PhotoImage(file = logo_filepath)
logo = tk.Label(root, image=img)
logo.photo = img
logo.grid(row=0, column=3, rowspan=10, columnspan=4)

tk.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多