-1
好了,我有这样的代码生成按钮的网格:通过这样Python,tkinter:如何更改新闻按钮的颜色?
def click():
squares[-1][y].configure(bg='blue')
def game(width,height):
global squares
squares = []
global y
for x in range(width):
squares.append([0] * height)
for y in range(height):
squares[-1][y] = Button(gameWindow,command=click)
squares[-1][y].grid(column = x, row = (y + 1), sticky =(N+S+E+W))
for x in range(width):
Grid.columnconfigure(gameWindow, x, weight = 1)
for y in range(height):
Grid.rowconfigure(gameWindow, (y + 1), weight = 1)
gameWindow.mainloop()
game(8,8)
我可以配置一个特定按钮(1,1):
squares[1][1].configure(bg='blue')
但是,当我尝试配置一个按钮时,它会改变右下角的按钮。
任何帮助将不胜感激,在此先感谢。
请提供[ MCVE。具体来说:会发生什么以及您预期会发生什么。 –
你有'在范围(高度)的y',所以在这个循环之后'y == height-1'。你需要使用'squares [-1] [height-1]' – furas
'Button(gameWindow,command = lambda arg = y:点击(arg))'和'def点击(arg):方块[-1] [arg] .config(bg ='blue')' – furas