Python标签图像不存在,但os.path表示确实存在

问题描述 投票:1回答:1

这是令我疯狂的代码:

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()
python tkinter label
1个回答
3
投票
Label(win,image="logo.gif").pack()

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

您可以使用标签显示PhotoImage和BitmapImage对象。这样做时,请确保保留对图像对象的引用,以防止Python的内存分配器对其进行垃圾回收。您可以使用全局变量或实例属性,或更简单的方法是,只需将属性添加到小部件实例:

photo = PhotoImage(file="icon.gif")
w = Label(parent, image=photo)
w.photo = photo
w.pack()
© www.soinside.com 2019 - 2024. All rights reserved.