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

时间:2014-05-07 17:44:00

标签: 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()

1 个答案:

答案 0 :(得分:3)

Label(win,image="logo.gif").pack()

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

  

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

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