Python使用tkinter加載png、jpg等圖片
首先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') # 打開圖片 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()
但運(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ǔ)充:解決python tkinter 展示jpg、png格式圖片的問(wèn)題
報(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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
TensorFlow Session使用的兩種方法小結(jié)
今天小編就為大家分享一篇TensorFlow Session使用的兩種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Python+django實(shí)現(xiàn)文件上傳
本系列以可操作性為主,介紹如何通過(guò)django web框架來(lái)實(shí)現(xiàn)一些簡(jiǎn)單的功能。每一篇文章都具有完整性和獨(dú)立性。使用新手在動(dòng)手做的過(guò)程中體會(huì)web開發(fā)的過(guò)程,過(guò)程中細(xì)節(jié)請(qǐng)參考相關(guān)文檔。2016-01-01python 定義給定初值或長(zhǎng)度的list方法
今天小編就為大家分享一篇python 定義給定初值或長(zhǎng)度的list方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06將labelme格式數(shù)據(jù)轉(zhuǎn)化為標(biāo)準(zhǔn)的coco數(shù)據(jù)集格式方式
今天小編就為大家分享一篇將labelme格式數(shù)據(jù)轉(zhuǎn)化為標(biāo)準(zhǔn)的coco數(shù)據(jù)集格式方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02Python實(shí)現(xiàn)讀取Linux系統(tǒng)的CPU以及內(nèi)存占用
這篇文章主要為大家詳細(xì)介紹了如何利用Python語(yǔ)言實(shí)現(xiàn)Linux系統(tǒng)的CPU以及內(nèi)存占用,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以收藏一下2023-05-05