这是令我疯狂的代码:
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
类,应改用该类。
您可以使用标签显示PhotoImage和BitmapImage对象。这样做时,请确保保留对图像对象的引用,以防止Python的内存分配器对其进行垃圾回收。您可以使用全局变量或实例属性,或更简单的方法是,只需将属性添加到小部件实例:
photo = PhotoImage(file="icon.gif")
w = Label(parent, image=photo)
w.photo = photo
w.pack()