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

标签 python tkinter label

这是让我发疯的代码:

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 参数不接受文件名。根据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()

关于Python 标签图像不存在,但 os.path 说它存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23524615/

相关文章:

json - 如何通过 REST 将标签添加到 Confluence 页面

python - 使用较小的 N 维数组按列对 N 维 numpy 数组进行排序

python sys.argv[1] 与 sys.argv[1 :]

python - 如何将 tkinter 窗口设置为恒定大小

Python 2.7 盲人可访问性

Python matplotlib colorbar 轴偏移字符串位置/位置

python - 格式化 pandas y 轴以显示时间而不是总秒数

python - 逐行、逐个单元地比较 2 个 Pandas 数据帧

python - 创建列表后关闭文件 > text = open ("path/file.ext").read().split()

python-3.x - Tkinter - 通过另一个功能更改标签文本