我正在制作一張隨機賓果卡,但我完全被這個問題困住了。我想制作一個點擊時會改變顏色的按鈕,但我似乎無法讓它自己定位。當我使用下面的代碼并單擊一個按鈕時,它只會更改右下角的按鈕(這是最后一個生成的)
def colourChange():
bingoButton.configure(bg="blue")
while whatever < 25:
whatever = 1
bingoButton = tk.Button(window,
text=whatever,
foreground="white",
background="black",
font=myFont,
command=colourChange
)
bingoButton.config(height=3, width=5)
bingoButton.grid(column=gridPosX,row=gridPosY)
gridPosX = 1
if gridPosX == 5:
gridPosX = 0
gridPosY = 1
許多變數和東西只是占位符,因為這只是一個測驗程式,可以在將其移動到主要專案之前讓我的方向正確。
uj5u.com熱心網友回復:
您可以在創建按鈕時將按鈕的單擊事件系結到回呼函式,例如 colourChange。
bingoButton.bind('<Button-1>', colourChange)
回呼函式需要更改為這樣的東西。
def colourChange(event):
# event.widget refers to the widget, e.g. button, that
# has triggered the event
event.widget['bg'] = 'blue'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/356573.html
