pythonはtkinterボタンに基づいて画像の切り替えを実現します。


tkinterはpythonの標準Tk GUIツールバッグのインターフェイスです。windowsの下でpython 3をインストールすれば、pythonをインストールする時、自動的にtkinterをインストールしました。
linuxシステムであれば、自動的にtkinterをインストールしません。

sudo apt-get install python-tk
手動でインストール
まず紹介します。tkitter自体はgifなどの少数の画像フォーマットしかサポートしていません。もし写真が複雑でないなら、直接右クリックして画像を編集し、絵の画面で画像をgif形式として保存すればいいです。本当に幻想的です


具体的なプログラミング操作
設定画像のコードを直接書き直してみたら問題があります。
たとえば

import tkinter as tk
top = tk.Tk()
 
top.title("    ")  #     
width = 260
height = 500
top.geometry(f'{width}x{height}')  #       
 
img_gif = tk.PhotoImage(file='./  /  .gif')  #     
label_img = tk.Label(top, image=img_gif)  #        
label_img.place(x=30, y=120)
 
def change_img():   #       
    img_gif0 = tk.PhotoImage(file='./  / .gif')
    label_img.configure(image=img_gif0) 
    label_img.place(x=30, y=120)
 
button = tk.Button(top, text='Prediction', command=change_img)  #     
button.place(x=90, y=330)
 
top.mainloop()
ここで直接ラベルを書き直しました。しかし、実際の効果は
疑問符.gifは正常に表示され、

ボタンをクリックして歩くと、gifが表示されません。

実際に画像を切り替えて、configurで実現すべきです。
正しい操作は以下の通りです。

import tkinter as tk
top = tk.Tk()
 
top.title("    ")  #     
width = 260
height = 500
top.geometry(f'{width}x{height}')  #       
 
img_gif = tk.PhotoImage(file='./  /  .gif')  #     
img_gif0 = tk.PhotoImage(file='./  / .gif')
 
label_img = tk.Label(top, image=img_gif)  #        
label_img.place(x=30, y=120)
 
def change_img():
    label_img.configure(image=img_gif0)  #       
 
button = tk.Button(top, text='Prediction', command=change_img)  #     
button.place(x=90, y=330)
 
top.mainloop()
具体的な効果

ボタンをクリックしたら

ここでは、pythonがtkinterのボタンを押して画像の切り替えを実現する文章について紹介します。これに関連して、python tkinterの画像の切り替えについては、以前の記事を検索したり、下記の関連記事を引き続き閲覧したりしてください。これからもよろしくお願いします。