首先PhotoImage注意這里只支持gif格式的圖片
photo = PhotoImage(file="D:/python/images/02.gif")
發(fā)現(xiàn)tkinter是只支持gif的格式,如果要加載png或者jpg的話就要使用PIL模塊
from tkinter import * from PIL import Image, ImageTk root = Tk() root.title('測(cè)試組python畢業(yè)題') img = Image.open('ques.png') # 打開(kāi)圖片 photo = ImageTk.PhotoImage(img) # 用PIL模塊的PhotoImage打開(kāi) 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()
但運(yùn)行時(shí)會(huì)報(bào)
ModuleNotFoundError: No module named 'PIL'
運(yùn)行命令:
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>
如果運(yùn)行該命令 顯示
Requirement already satisfied: Pillow in c:\program files (x86)\python\lib\site-packages (3.4.2)
則表示已經(jīng)安裝過(guò)了
如果已安裝則先卸載以獲取最新的pillow
運(yùn)行命令: pip uninstall pillow
然后運(yùn)行:pip install pillow
就可以了
報(bào)錯(cuò):
from tkinter import * img = PhotoImage(file = r'D:\test\hero\暗黑元首\暗黑元首.jpg') lable_show = Label(frame_show,imag = img)
解決:首先安裝PIL庫(kù),使用pip命令
pip install pillow
然后使用PIL庫(kù)獲得ImageTk.PhotoImage對(duì)象代替tk.PhotoImage對(duì)象即可
from PIL import Image,ImageTk img = ImageTk.PhotoImage(Image.open(r'D:\test\hero\暗黑元首\暗黑元首.jpg')) lable_show = Label(frame_show,imag = img)
到此這篇關(guān)于Python使用tkinter加載png、jpg等圖片的文章就介紹到這了,更多相關(guān)tkinter加載png、jpg內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:衡水 江蘇 呼和浩特 湖州 股票 中山 駐馬店 畢節(jié)
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python使用tkinter加載png、jpg等圖片》,本文關(guān)鍵詞 Python,使用,tkinter,加載,png,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。