使用tkinter加载png,jpg


最近来使用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图片

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM