好吧,我遇到的問題是我不明白如何引用被按下的特定按鈕。我在做一個遊戲,如果使用點擊一個按鈕我想知道哪個X和Y座標。所以,如果你有一個按鈕的網格,你點擊一個我想要的座標,然後我會改變該按鈕的顏色。 :)參考一個按鈕然後配置它
問題:知道哪個按鈕在網格中按下。
在此先感謝!
def matris():
for i in range(5):
newButton = Button(app, width = 4, height = 2, bg = "blue",command = lambda i=i: function(i))
newButton.grid(row = i, column = 0)
for i in range(5):
newButton = Button(app, width = 4, height = 2, bg = "blue",command = lambda i=i + 5: function(i))
newButton.grid(row = i, column = 1)
for i in range(5):
newButton = Button(app, width = 4, height = 2, bg = "blue",command = lambda i=i + 10: function(i))
newButton.grid(row = i, column = 2)
for i in range(5):
newButton = Button(app, width = 4, height = 2, bg = "blue",command = lambda i=i + 15: function(i))
newButton.grid(row = i, column = 3)
for i in range(5):
newButton = Button(app, width = 4, height = 2, bg = "blue",command = lambda i=i + 20: function(i))
newButton.grid(row = i, column = 4)
def function(i):
if button 23 was clicked.changeColor to e.g "blue"
你能解釋你想用'command = lambda i = i + 20:function(i) '?我不認爲這是有效的Python語法。 – Kevin
這似乎是部分僞代碼。這是你實際寫的嗎? – Tadgh
@Kevin:這是非常好的Python代碼,你爲什麼認爲這是錯的?無可否認,它可能會寫得更清晰一些,比如'lambda widget_number = i + 20:function(widget_number)',但無論哪種方式都有效。 –