前提・実現したいこと
tkinterでbuttonを設置しようとしています。
複数の画像つきbuttonを設置したいので、for文を用いて処理したいのですが、逐次書いた場合とfor文で書いた場合で結果が異なります。
該当のソースコード
import tkinter as tk root = tk.Tk() root.title("Equity Calculater") root.geometry("800x600") c1 = tk.PhotoImage(file="card/1c.png", master = root) button = tk.Button(root, image=c1, command="pushed") button.grid(row=0, column=0) d1 = tk.PhotoImage(file="card/1s.png", master = root) button = tk.Button(root, image=d1, command="pushed") button.grid(row=1, column=0) h1 = tk.PhotoImage(file="card/1d.png", master = root) button = tk.Button(root, image=h1, command="pushed") button.grid(row=2, column=0) s1 = tk.PhotoImage(file="card/1h.png", master = root) button = tk.Button(root, image=s1, command="pushed") button.grid(row=3, column=0)
このコードは正しく動作しますが、
deck = ['card/1c.png', 'card/1d.png', 'card/1h.png', 'card/1s.png'] photo = ['c1', 'd1', 'h1', 's1'] for i, (photo_, deck_) in enumerate(zip(photo,deck)): photo_ = tk.PhotoImage(file=deck_, master = root) button = tk.Button(root, image=photo_, command="pushed") button.grid(row=i, column=0)
とすると、表示されるボタンは"s1"のみで、他は画像が表示されません。
補足情報(FW/ツールのバージョンなど)
逐次書いた場合の結果の画像:
for文で書いた場合の結果の画像:
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/02 19:54
2019/07/02 20:43