成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術文章
文章詳情頁

詳解python tkinter 圖片插入問題

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

通過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') 中的關鍵字file不能夠省略,否則程序無法正常顯示圖片。

對于常用的PNG,與JPG格式的圖片,我們需要從python image library(pillow)(PIL)導入Image與ImageTk模塊來實現(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()

然而在實際操作中,本人使用的是Anaconda spyder編譯器,當我們在讀入圖像時程序出錯后,再次運行程序就會導致image 'pyimage1' doesn’t exist錯誤,每次運行一次,數(shù)字就會增加1,如:image 'pyimage2' doesn’t exist。遇到此錯誤,可以直接在IPython控制臺界面重啟IPython內(nèi)核即可,或者關閉編譯器并重新打開。

看似我們已經(jīng)完全實現(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界面的圖像插入部分分離到另一個函數(shù)中,并且直接定義一個tkinter的類,這樣做的好處是我們可以直接用self替代創(chuàng)建的主窗口界面,并且我們可以在不同的地方啟動主循環(huán),self.mainloop()和main.mainloop()任選一個即可。并且因為我們想要插入圖片,所以我們可以省略指定Gui界面的尺寸,這樣做的好處是會創(chuàng)建一個自適應圖片大小的Gui界面。最重要的是我們可以看到多了一行代碼label.image = photo,我們將選取的圖片photo賦值給了label的屬性對象image,如果沒有這一行代碼,圖片便無法正常顯示,這是因為python會自動回收不使用的對象,所以我們需要使用屬性對象進行聲明。 上述的程序隱患便是因為缺少了這一行代碼。

至此,tkinter的圖片插入可暫時告一段落。

到此這篇關于詳解python tkinter 圖片插入問題的文章就介紹到這了,更多相關python tkinter 圖片插入內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 曰本美女高清在线观看免费 | 国产成人aaa在线视频免费观看 | 99je全部都是精品视频在线 | 免费观看女人一摸全是水 | 99精品久久久久久久免费看蜜月 | 国产亚洲高清在线精品99 | 久久精品国产亚洲网址 | 97在线视频免费播放 | 免费观看情趣v视频网站 | 国产成人免费高清视频网址 | 99久久99这里只有免费费精品 | 亚洲欧美在线免费观看 | 国产韩国精品一区二区三区久久 | 欧美综合一区二区三区 | 成人a免费视频播放 | 在线观看视频亚洲 | 国内自拍网站 | 请看一下欧美一级毛片 | 免费一级特黄 欧美大片 | 国产一级一国产一级毛片 | 国产精品欧美日韩一区二区 | 亚洲一区二区三区精品影院 | 性欧美视频a毛片在线播放 性欧美一级 | 精品亚洲福利一区二区 | 日韩色视频一区二区三区亚洲 | 国产精品免费大片一区二区 | 国产成人综合亚洲 | 91欧美亚洲| 日韩中文字幕一在线 | 国产精品区牛牛影院 | 九九色综合网 | 一级黄色香蕉视频 | 免费观看一级成人毛片软件 | 成人在线视频国产 | 成人看片在线观看免费 | 中文字幕咪咪网 | 天天躁日日躁狠狠躁一级毛片 | 一级毛片在线播放免费 | 美女张腿男人桶免费视频 | 日本高清无吗免费播放 | 国产成人永久免费视频 |