首先PhotoImage注意这里只支持gif格式的图片

photo = PhotoImage(file="D:/python/images/02.gif")

发现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()

但运行时会报
ModuleNotFoundError: No module named 'PIL’

运行命令:
pip install pillow

D:\Program Files\Python37>pip install pillow
Collecting pillow
  Downloading https://files.pythonhosted.org/packages/40/f2/a424d4d5dd6aa8c26636969decbb3da1c01286d344e71429b1d648bccb64/Pillow-6.0.0-cp37-cp37m-win_amd64.whl (2.0MB)
     |████████████████████████████████| 2.0MB 133kB/s
Installing collected packages: pillow
Successfully installed pillow-6.0.0

D:\Program Files\Python37>

如果运行该命令 显示
Requirement already satisfied: Pillow in c:\program files (x86)\python\lib\site-packages (3.4.2)
则表示已经安装过了

如果已安装则先卸载以获取最新的pillow
运行命令: pip uninstall pillow
然后运行:pip install pillow
就可以了

参考:https://www.cnblogs.com/semishigure/p/8124397.html
参考:https://blog.csdn.net/jacabe/article/details/80040338

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐