国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

詳解python tkinter 圖片插入問(wèn)題

瀏覽:3日期:2022-07-12 13:27:58

通過(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)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 长子县| 巴青县| 霞浦县| 广水市| 通渭县| 栾城县| 龙江县| 理塘县| 广灵县| 胶南市| 张家界市| 中牟县| 田林县| 秀山| 阜宁县| 新昌县| 宝兴县| 惠水县| 金坛市| 武穴市| 垫江县| 星子县| 喀什市| 东乡族自治县| 禹城市| 临沂市| 香格里拉县| 革吉县| 稻城县| 罗山县| 肃宁县| 富民县| 台北县| 吴川市| 江城| 于都县| 白山市| 山东省| 林芝县| 聊城市| 永安市|