久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

python基于tkinter點(diǎn)擊按鈕實(shí)現(xiàn)圖片的切換

瀏覽:105日期:2022-06-23 08:44:01

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)魔幻)

python基于tkinter點(diǎn)擊按鈕實(shí)現(xiàn)圖片的切換

python基于tkinter點(diǎn)擊按鈕實(shí)現(xià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能夠正常顯示,

python基于tkinter點(diǎn)擊按鈕實(shí)現(xiàn)圖片的切換

點(diǎn)擊按鈕后,走.gif無法顯示

python基于tkinter點(diǎn)擊按鈕實(shí)現(xiàn)圖片的切換

實(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()

具體效果

python基于tkinter點(diǎn)擊按鈕實(shí)現(xiàn)圖片的切換

點(diǎn)擊按鈕后

python基于tkinter點(diǎn)擊按鈕實(shí)現(xià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)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 东乡| 平罗县| 冷水江市| 襄樊市| 宜良县| 雷波县| 湘潭市| 佳木斯市| 奉化市| 大连市| 拜城县| 平南县| 乾安县| 克拉玛依市| 河北区| 东平县| 阿拉尔市| 中江县| 宾川县| 灌南县| 濮阳市| 仁怀市| 阜平县| 土默特右旗| 应用必备| 吴江市| 南丰县| 浦县| 宁南县| 桑植县| 慈利县| 尚志市| 九台市| 襄汾县| 鹤岗市| 平塘县| 铜鼓县| 松潘县| 梨树县| 镇康县| 大田县|