[英]Python Label image doesn't exist but os.path says it does
Here is the code that is driving me crazy:这是让我发疯的代码:
from tkinter import*
import os.path
class About:
def __init__(self):
font1='tahoma 12'
win=Tk()
print(os.path.isfile('logo.gif'))#It returns true
Label(win,image="logo.gif").pack()
About()
Label(win,image="logo.gif").pack()
The image
parameter won't accept a filename. image
参数不接受文件名。 According to this tutorial, "the value should be a PhotoImage, BitmapImage, or a compatible object."根据本教程,“该值应该是 PhotoImage、BitmapImage 或兼容对象。” It goes on to discuss the
PhotoImage
class, which you should use instead.它继续讨论
PhotoImage
类,您应该使用它。
You can use the label to display PhotoImage and BitmapImage objects.
您可以使用标签来显示 PhotoImage 和 BitmapImage 对象。 When doing this, make sure you keep a reference to the image object, to prevent it from being garbage collected by Python's memory allocator.
执行此操作时,请确保保留对图像对象的引用,以防止 Python 的内存分配器对其进行垃圾回收。 You can use a global variable or an instance attribute, or easier, just add an attribute to the widget instance:
您可以使用全局变量或实例属性,或者更简单,只需向小部件实例添加一个属性:
photo = PhotoImage(file="icon.gif")
w = Label(parent, image=photo)
w.photo = photo
w.pack()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.