0
我正在創建一個記憶遊戲,並遇到按鈕的問題。我使用for循環創建按鈕並將它們放入數組中,但我不知道如何爲它們中的每一個調用定義OnButtonClick。每個按鈕應該有一個隨機圖片,從八個選項中選擇,不超過兩個副本。與Tkinter的Python記憶遊戲 - 定義和按鈕的問題
from Tkinter import *
import Tkinter
import random
root = Tkinter.Tk()
poop=PhotoImage(file="poop.gif")
apple=PhotoImage(file="apple.gif")
earth=PhotoImage(file="earth.gif")
fish=PhotoImage(file="fish.gif")
frowny=PhotoImage(file="frowny.gif")
heart=PhotoImage(file="heart.gif")
smiley=PhotoImage(file="images.gif")
water=PhotoImage(file="water.gif")
back=PhotoImage(file="card back.gif")
images = [poop,apple,earth,fish,frowny,heart,smiley,water]
row = 1
column = 0
buttonList=[]
def OnButtonClick():
self.Button.config(image=random.choice(images))
for i in range(16):
buttonList.append(Button(root, image=back, width=150,height=250,command=OnButtonClick()))
buttonList[i].grid(row = row,column = column)
column += 1
if column == 4:
column = 0
row += 1
root.mainloop()
如何在按下按鈕時更改圖片?
你生成它們以同樣的方式,你需要去通過接入數組中的每個(特定元素)元素都可以更改圖像。 – Afflicted