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í)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例2. python中PyQuery庫用法分享3. python操作數(shù)據(jù)庫獲取結(jié)果之fetchone和fetchall的區(qū)別說明4. python 爬取嗶哩嗶哩up主信息和投稿視頻5. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)6. AJAX實(shí)現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺(tái)】7. 關(guān)于HTML5的img標(biāo)簽8. php5.6不能擴(kuò)展redis.so的解決方法9. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值10. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效
