python3 tk 配置した画像をボタンにしたいが、
コマンドのつけ方が分かりません。
commandの追加方法など教えて下さい
現在のコード
python
1 2 3import tkinter 4 5# マップの描画 6def draw_map(): 7 for y in range(0, MAX_HEIGHT): 8 for x in range(0, MAX_WIDTH): 9 p = map_data[y][x] 10 canvas.create_image(x*64+30, y*64+30, image=images[p]) 11 12 13 14# ウィンドウ作成 15root = tkinter.Tk() 16root.title("パイソン") 17root.minsize(330, 330) 18 19# キャンバス作成 20canvas = tkinter.Canvas(width=330, height=330) 21canvas.place(x=5, y=5) 22 23 24 25# 画像データを読み込み 26images = [ 27tkinter.PhotoImage(file="image01.png"), 28tkinter.PhotoImage(file="image02.png"), 29tkinter.PhotoImage(file="image03.png"), 30tkinter.PhotoImage(file="image04.png"), 31tkinter.PhotoImage(file="image05.png") 32] 33 34# マップデータ 35MAX_WIDTH = 5 36MAX_HEIGHT = 5 37map_data = [ 38 [0, 1, 2, 3, 4], 39 [0, 1, 2, 3, 4], 40 [0, 1, 2, 3, 4], 41 [0, 1, 2, 3, 4], 42 [0, 1, 2, 3, 4], 43] 44 45 46 47draw_map() 48root.mainloop() 49
通常のボタン
python
1 2def push1(): 3 print('hello') 4 5Button01 = tk.Button(root, text='Button01' , command = push1) 6Button01.place() 7
失敗したコード冒頭
python
1 2def push1(): 3 print('hello') 4 5 6import tkinter 7 8# マップの描画 9def draw_map(): 10 for y in range(0, MAX_HEIGHT): 11 for x in range(0, MAX_WIDTH): 12 p = map_data[y][x] 13 canvas.create_image(x*64+30, y*64+30, image=images[p] 14 ,command=push1)
エラー
_tkinter.TclError: unknown option "-command"
#解糖を受けての追記
python
1def click(event): 2 print('Hello,World') 3 # クリックされた場所に描画する 4 5import tkinter 6 7# マップの描画 8def draw_map(): 9 for y in range(0, MAX_HEIGHT): 10 for x in range(0, MAX_WIDTH): 11 p = map_data[y][x] 12 canvas.create_image(x*64+30, y*64+30, image=images[p]) 13 #イベントを設定する 14 canvas.bind("<Button-1>", click) 15
上記のコードで実行を確認できました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。