詳解python tkinter 圖片插入問(wèn)題
通過(guò)tkinter.PhotoImage插入GIF, PGM/PPM格式的圖片。
import tkinterclass Gui: def __init__(self): self.gui=tkinter.Tk()# create gui window self.gui.title('Image Display') # set the title of gui self.gui.geometry('800x600') # set the window size of gui img = tkinter.PhotoImage(file='C:/Users/15025/Desktop/bear.gif') # read image from path label1=tkinter.Label(self.gui,image=img) # create a label to insert this image label1.grid() # set the label in the main window self.gui.mainloop() # start mainloopmain = Gui()
注意: img = tkinter.PhotoImage(file='C:/Users/15025/Desktop/bear.gif') 中的關(guān)鍵字file不能夠省略,否則程序無(wú)法正常顯示圖片。
對(duì)于常用的PNG,與JPG格式的圖片,我們需要從python image library(pillow)(PIL)導(dǎo)入Image與ImageTk模塊來(lái)實(shí)現(xiàn),代碼如下:
import tkinterfrom PIL import Imagefrom PIL import ImageTkclass Gui: def __init__(self): self.gui=tkinter.Tk()# create gui window self.gui.title('Image Display') # set the title of gui self.gui.geometry('800x600') # set the window size of gui load = Image.open('C:/Users/15025/Desktop/1.png') # open image from path img = ImageTk.PhotoImage(load) # read opened image label1=tkinter.Label(self.gui,image=img) # create a label to insert this image label1.grid() # set the label in the main window self.gui.mainloop() # start mainloopmain = Gui()
然而在實(shí)際操作中,本人使用的是Anaconda spyder編譯器,當(dāng)我們?cè)谧x入圖像時(shí)程序出錯(cuò)后,再次運(yùn)行程序就會(huì)導(dǎo)致image 'pyimage1' doesn’t exist錯(cuò)誤,每次運(yùn)行一次,數(shù)字就會(huì)增加1,如:image 'pyimage2' doesn’t exist。遇到此錯(cuò)誤,可以直接在IPython控制臺(tái)界面重啟IPython內(nèi)核即可,或者關(guān)閉編譯器并重新打開(kāi)。
看似我們已經(jīng)完全實(shí)現(xiàn)了圖片的插入,但是這種插入方法是存在隱患的,具體代碼如下:
import tkinter as tkfrom PIL import Imagefrom PIL import ImageTkclass Gui(tk.Tk): def __init__(self): super().__init__() self.title('Figure dynamic show v1.01') # self.geometry('1000x800+400+100') self.mainGui() # self.mainloop() def mainGui(self): image = Image.open('C:/Users/15025/Desktop/1.png') photo = ImageTk.PhotoImage(image) label = tk.Label(self, image=photo) label.image = photo # in case the image is recycled label.grid() main = Gui()main.mainloop()
這里我們可以看到相比較上面的程序,我們將Gui界面的圖像插入部分分離到另一個(gè)函數(shù)中,并且直接定義一個(gè)tkinter的類(lèi),這樣做的好處是我們可以直接用self替代創(chuàng)建的主窗口界面,并且我們可以在不同的地方啟動(dòng)主循環(huán),self.mainloop()和main.mainloop()任選一個(gè)即可。并且因?yàn)槲覀兿胍迦雸D片,所以我們可以省略指定Gui界面的尺寸,這樣做的好處是會(huì)創(chuàng)建一個(gè)自適應(yīng)圖片大小的Gui界面。最重要的是我們可以看到多了一行代碼label.image = photo,我們將選取的圖片photo賦值給了label的屬性對(duì)象image,如果沒(méi)有這一行代碼,圖片便無(wú)法正常顯示,這是因?yàn)閜ython會(huì)自動(dòng)回收不使用的對(duì)象,所以我們需要使用屬性對(duì)象進(jìn)行聲明。 上述的程序隱患便是因?yàn)槿鄙倭诉@一行代碼。
至此,tkinter的圖片插入可暫時(shí)告一段落。
到此這篇關(guān)于詳解python tkinter 圖片插入問(wèn)題的文章就介紹到這了,更多相關(guān)python tkinter 圖片插入內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 淺談python多線程和多線程變量共享問(wèn)題介紹2. .NET6打包部署到Windows Service的全過(guò)程3. WML語(yǔ)言的基本情況4. CSS代碼檢查工具stylelint的使用方法詳解5. 利用CSS制作3D動(dòng)畫(huà)6. Python 多線程之threading 模塊的使用7. 刪除docker里建立容器的操作方法8. XML入門(mén)的常見(jiàn)問(wèn)題(四)9. ThinkPHP5 通過(guò)ajax插入圖片并實(shí)時(shí)顯示(完整代碼)10. Properties 持久的屬性集的實(shí)例詳解
