python基于tkinter點(diǎn)擊按鈕實(shí)現(xiàn)圖片的切換
tkinter是python的標(biāo)準(zhǔn)Tk GUI工具包的接口,在windows下如果你安裝的python3,那在安裝python的時(shí)候,就已經(jīng)自動(dòng)安裝了tkinter了
如果是在linux系統(tǒng)中,則不會(huì)自動(dòng)安裝tkinter,需要通過
sudo apt-get install python-tk手動(dòng)安裝
首先先介紹一下,tkinter本身只支持gif等少數(shù)幾個(gè)圖片格式,如果圖片并不復(fù)雜,建議直接右擊圖片,進(jìn)入編輯,在畫圖界面將圖片另存為gif格式就可以使用了(連png和jpeg都不支持。。。真的有點(diǎn)魔幻)
具體的編程操作
如果你嘗試直接重寫設(shè)置圖片的有關(guān)代碼會(huì)出問題
比如
import tkinter as tktop = tk.Tk() top.title('劃水摸魚') # 設(shè)置窗口width = 260height = 500top.geometry(f’{width}x{height}’) # 設(shè)置窗口大小 img_gif = tk.PhotoImage(file=’./動(dòng)作/問號(hào).gif’) # 設(shè)置圖片label_img = tk.Label(top, image=img_gif) # 設(shè)置預(yù)顯示圖片label_img.place(x=30, y=120) def change_img(): # 設(shè)置按鈕事件 img_gif0 = tk.PhotoImage(file=’./動(dòng)作/走.gif’) label_img.configure(image=img_gif0) label_img.place(x=30, y=120) button = tk.Button(top, text=’Prediction’, command=change_img) # 設(shè)置按鈕button.place(x=90, y=330) top.mainloop()
在這里我直接重寫了label_img,但是實(shí)際效果是
問號(hào).gif能夠正常顯示,
點(diǎn)擊按鈕后,走.gif無法顯示
實(shí)際切換圖片,應(yīng)該用configure實(shí)現(xiàn)
正確的操作如下
import tkinter as tktop = tk.Tk() top.title('劃水摸魚') # 設(shè)置窗口width = 260height = 500top.geometry(f’{width}x{height}’) # 設(shè)置窗口大小 img_gif = tk.PhotoImage(file=’./動(dòng)作/問號(hào).gif’) # 設(shè)置圖片img_gif0 = tk.PhotoImage(file=’./動(dòng)作/走.gif’) label_img = tk.Label(top, image=img_gif) # 設(shè)置預(yù)顯示圖片label_img.place(x=30, y=120) def change_img(): label_img.configure(image=img_gif0) # 設(shè)置按鈕事件 button = tk.Button(top, text=’Prediction’, command=change_img) # 設(shè)置按鈕button.place(x=90, y=330) top.mainloop()
具體效果
點(diǎn)擊按鈕后
到此這篇關(guān)于python基于tkinter點(diǎn)擊按鈕實(shí)現(xiàn)圖片的切換的文章就介紹到這了,更多相關(guān)python tkinter 圖片切換內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 解決Python數(shù)據(jù)可視化中文部分顯示方塊問題2. python開發(fā)一個(gè)解析protobuf文件的簡單編譯器3. ASP編碼必備的8條原則4. python 根據(jù)列表批量下載網(wǎng)易云音樂的免費(fèi)音樂5. python中round函數(shù)保留兩位小數(shù)的方法6. python 基于AioHttp 異步抓取火星圖片7. 利用python+ffmpeg合并B站視頻及格式轉(zhuǎn)換的實(shí)例代碼8. Android實(shí)現(xiàn)儀表盤控件開發(fā)9. Python基于traceback模塊獲取異常信息10. 深入理解JavaScript字節(jié)二進(jìn)制知識(shí)以及相關(guān)API
