简体   繁体   English

图像未显示在 Tkinter 标签小部件中

[英]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.

相关问题 使用标签小部件使用 Tkinter 显示图像 - Displaying Image with Tkinter Using Label Widget Tkinter 图像未显示在标签或画布中 - Tkinter Image not displaying in label or canvas Tkinter Label 小部件显示出 alignment 与其他 ZD304BA20E96D874341588EEABAC80 小部件? - Tkinter Label widget displaying out of alignment with other label widgets? 图像未通过标签小部件显示在Tkinter上 - Image not getting displayed on Tkinter through Label widget 如何更新 Tkinter 标签小部件的图像? - How to update the image of a Tkinter Label widget? 从Python的tkinter条目小部件接收输入并将其显示在标签中 - Receiveing input from Python's tkinter entry widget and displaying it in a label 将图像用作类对象属性,然后在Tkinter标签小部件中打开该图像 - Using an image as an class object attribute, then opening that image in a Tkinter label widget Tkinter标签小部件未更新 - Tkinter Label widget is not updated Python / Tkinter:在“标签”小部件中自定义图像和文本之间的水平填充? - Python/Tkinter: Customize horizontal padding between image and text in Label widget? 除非消息窗口小部件位于其后面,否则不会绘制Python tkinter标签图像 - Python tkinter label image not drawn unless a message widget is behind it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM