[英]Image not displaying in Tkinter Label widget
I'm trying to get an image to display in a Tkinter Label widget.我正在尝试让图像显示在 Tkinter 标签小部件中。 This code works inside a class in PyCharm, but doesn't get past the 'tk.Label' line in the main app.
这段代码在 PyCharm 的一个类中工作,但不会超过主应用程序中的“tk.Label”行。 I've consulted other answers here but haven't been able to figure out why the image isn't displaying in the main app.
我在这里咨询了其他答案,但无法弄清楚为什么图像没有显示在主应用程序中。
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)
It's a very simple error.这是一个非常简单的错误。 Just make sure that you aren't defining
self.[Your Variable]
outside of a class.只要确保您没有在类之外定义
self.[Your Variable]
。 Because self
is only available in classes.因为
self
只在类中可用。 Also, Here's my code:另外,这是我的代码:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.