前提・実現したいこと
pythonのtkinterでguiアプリを作っています。ボタンを押したら画面遷移(現在のframeを削除して新しいframeを作ることで画面が切り替わっているように見せている)する機能があります。
遷移前では問題なく画像をcanvasに反映できています。
遷移先のframeでcanvasを配置できているのですが画像が反映されません。エラーも出ません。
遷移先のcanvasに画像を反映させるにはどうすればいいですか?よろしくお願いします。
発生している問題・エラーメッセージ
該当のソースコード
Python
1import tkinter as tk 2 3 4def btn_clicked(): 5 print("Button Clicked") 6 7def second(): 8 global frame2 9 frame1.destroy() 10 frame2 = tk.Frame( 11 root, 12 width=1000, 13 height=600 14 ) 15 frame2.place(x=0, y=0) 16 17 canvas2 = tk.Canvas( 18 frame2, 19 bg="#ff4500", 20 height=600, 21 width=1000, 22 bd=0, 23 highlightthickness=0, 24 relief="ridge") 25 canvas2.place(x=0, y=0) 26 bg_img = tk.PhotoImage(file="background2.png") 27 canvas2.create_image( 28 500.0, 300.0, 29 image=bg_img) 30 31 btn_img = tk.PhotoImage(file="button1.png") 32 button1 = tk.Button( 33 frame2, 34 image=btn_img, 35 borderwidth=0, 36 highlightthickness=0, 37 command=btn_clicked, 38 relief="flat") 39 button1.place( 40 x=375, y=337, 41 width=250, 42 height=49) 43 44 45if __name__ == "__main__": 46 root = tk.Tk() 47 root.title("test") 48 root.geometry("1000x600") 49 root.configure(bg="#ffffff") 50 51 frame1 = tk.Frame( 52 root, 53 width=1000, 54 height=600 55 ) 56 frame1.place(x=0, y=0) 57 58 canvas = tk.Canvas( 59 frame1, 60 bg = "#ab82ff", 61 width=1000, 62 height=600, 63 bd=0, 64 highlightthickness=0, 65 relief="ridge") 66 canvas.place(x=0, y=0) 67 bg_img = tk.PhotoImage(file="background1.png") 68 canvas.create_image( 69 500.0, 300.0, 70 image=bg_img) 71 72 btn_img = tk.PhotoImage(file="button1.png") 73 button1 = tk.Button( 74 frame1, 75 image=btn_img, 76 borderwidth=0, 77 highlightthickness=0, 78 command=second, 79 relief="flat") 80 button1.place( 81 x=375, y=337, 82 width=250, 83 height=49) 84 85 root.resizable(False, False) 86 root.mainloop()
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
Python 3.8.10

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/31 03:53