詳解python tkinter 圖片插入問題
通過tkinter.PhotoImage插入GIF, PGM/PPM格式的圖片。
import tkinter class 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 mainloop main = Gui()
注意: img = tkinter.PhotoImage(file="C:/Users/15025/Desktop/bear.gif") 中的關(guān)鍵字file不能夠省略,否則程序無法正常顯示圖片。
對于常用的PNG,與JPG格式的圖片,我們需要從python image library(pillow)(PIL)導(dǎo)入Image與ImageTk模塊來實(shí)現(xiàn),代碼如下:
import tkinter from PIL import Image from PIL import ImageTk class 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 mainloop main = Gui()
然而在實(shí)際操作中,本人使用的是Anaconda spyder編譯器,當(dāng)我們在讀入圖像時(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控制臺界面重啟IPython內(nèi)核即可,或者關(guān)閉編譯器并重新打開。
看似我們已經(jīng)完全實(shí)現(xiàn)了圖片的插入,但是這種插入方法是存在隱患的,具體代碼如下:
import tkinter as tk from PIL import Image from PIL import ImageTk class 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的類,這樣做的好處是我們可以直接用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的屬性對象image,如果沒有這一行代碼,圖片便無法正常顯示,這是因?yàn)閜ython會(huì)自動(dòng)回收不使用的對象,所以我們需要使用屬性對象進(jìn)行聲明。 上述的程序隱患便是因?yàn)槿鄙倭诉@一行代碼。
至此,tkinter的圖片插入可暫時(shí)告一段落。
到此這篇關(guān)于詳解python tkinter 圖片插入問題的文章就介紹到這了,更多相關(guān)python tkinter 圖片插入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python 插入Null值數(shù)據(jù)到Postgresql的操作
- python 在mysql中插入null空值的操作
- Python xlwings插入Excel圖片的實(shí)現(xiàn)方法
- python中的插入排序的簡單用法
- python 使用xlsxwriter循環(huán)向excel中插入數(shù)據(jù)和圖片的操作
- python簡單實(shí)現(xiàn)插入排序?qū)嵗a
- Python操控mysql批量插入數(shù)據(jù)的實(shí)現(xiàn)方法
- Python操作word文檔插入圖片和表格的實(shí)例演示
- Python 如何在字符串中插入變量
- 如何用python插入獨(dú)創(chuàng)性聲明
相關(guān)文章
Python hashlib庫數(shù)據(jù)安全加密必備指南
這篇文章主要為大家介紹了Python hashlib庫數(shù)據(jù)安全加密的使用實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01手把手教你實(shí)現(xiàn)Python連接數(shù)據(jù)庫并快速取數(shù)的工具
在數(shù)據(jù)生產(chǎn)應(yīng)用部門,取數(shù)分析是一個(gè)很常見的需求,實(shí)際上業(yè)務(wù)人員需求時(shí)刻變化,最高效的方式是讓業(yè)務(wù)部門自己來取。本文就來手把手教大家搭建一個(gè)?Python?連接數(shù)據(jù)庫,快速取數(shù)工具,需要的可以參考一下2022-11-11使用 Python 和 Selenium 解決 Cloudflare&
Cloudflare 驗(yàn)證碼是一種用于區(qū)分人類用戶和自動(dòng)化機(jī)器人的功能,它是 Cloudflare 安全服務(wù)的重要組成部分,旨在防御網(wǎng)站免受自動(dòng)化攻擊和濫用,這篇文章主要介紹了使用 Python 和 Selenium 解決 Cloudflare 驗(yàn)證碼,需要的朋友可以參考下2024-06-06解決Python3.7.0 SSL低版本導(dǎo)致Pip無法使用問題
這篇文章主要介紹了解決Python3.7.0 SSL低版本導(dǎo)致Pip無法使用問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09Python基礎(chǔ)之?dāng)?shù)據(jù)類型知識匯總
今天帶大家復(fù)習(xí)一下Python基礎(chǔ)知識,文中對數(shù)據(jù)類型相關(guān)知識做了詳細(xì)的匯總,對剛?cè)腴Tpython的小伙伴很有幫助喲,需要的朋友可以參考下2021-05-05關(guān)于pytorch求導(dǎo)總結(jié)(torch.autograd)
這篇文章主要介紹了關(guān)于pytorch求導(dǎo)總結(jié)(torch.autograd),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Python pandas 的索引方式 data.loc[],data[][]示例詳解
這篇文章主要介紹了Python pandas 的索引方式 data.loc[], data[][]的相關(guān)資料,其中data.loc[index,column]使用.loc[ ]第一個(gè)參數(shù)是行索引,第二個(gè)參數(shù)是列索引,本文結(jié)合實(shí)例代碼講解的非常詳細(xì),需要的朋友可以參考下2023-02-02Pytorch參數(shù)注冊和nn.ModuleList nn.ModuleDict的問題
這篇文章主要介紹了Pytorch參數(shù)注冊和nn.ModuleList nn.ModuleDict的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01