最近来使用tkinter加载图片时遇到了困难,按照资料写了
photo = PhotoImage(file='ques.png')
imglabel = Label(root, image=photo)
imglabel.grid(row=0, column=0, columnspan=3)
却意外报错
经过多种尝试无果,最后发现tkinter是只支持gif的格式,如果要加载png或者jpg的话就要使用PIL模块:
from Tkinter import *
from PIL import Image, ImageTk root = Tk()
root.title('测试组python毕业题') img = Image.open('ques.png') # 打开图片
photo = ImageTk.PhotoImage(img) # 用PIL模块的PhotoImage打开
imglabel = Label(root, image=photo)
imglabel.grid(row=0, column=0, columnspan=3) Label(root, text="Answer:").grid(row=1, column=0, sticky=S + N) answerEntry = Entry(root)
btn = Button(root, text="Submit", command=submit) answerEntry.grid(row=1, column=1)
btn.grid(row=1, column=2) mainloop()
最后正确显示了png图片