繁体   English   中英

Python 标签图像不存在,但 os.path 说它存在

[英]Python Label image doesn't exist but os.path says it does

这是让我发疯的代码:

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()

image参数不接受文件名。 根据教程,“该值应该是 PhotoImage、BitmapImage 或兼容对象。” 它继续讨论PhotoImage类,您应该使用它。

您可以使用标签来显示 PhotoImage 和 BitmapImage 对象。 执行此操作时,请确保保留对图像对象的引用,以防止 Python 的内存分配器对其进行垃圾回收。 您可以使用全局变量或实例属性,或者更简单,只需向小部件实例添加一个属性:

photo = PhotoImage(file="icon.gif")
w = Label(parent, image=photo)
w.photo = photo
w.pack()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM