Python标签图像不存在,但操作系统路径说是的

2024-04-25 08:06:31 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是让我发疯的代码:

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

Tags: path代码fromimportselfinitostkinter
1条回答
网友
1楼 · 发布于 2024-04-25 08:06:31
Label(win,image="logo.gif").pack()

image参数不接受文件名。根据this教程,“值应该是PhotoImage、BitmapImage或兼容对象。”接着讨论了PhotoImage类,您应该改用它。你知道吗

You can use the label to display PhotoImage and BitmapImage objects. 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. 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()

相关问题 更多 >

    热门问题