好的,所以我有这个代码会在tkinter窗口中生成一个按钮网格,但是我没办法分开网格中的按钮。如果我想让按钮处于网格位置(4,4)蓝色,我该怎么做?我需要使用列表吗?我确信这是一个快速修复,提前谢谢。
def game(width,height):
for x in range(width):
for y in range(height):
square = Button(gameWindow)
square.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)
答案 0 :(得分:0)
将square
个对象放在列表列表中,然后您可以通过x
和y
from Tkinter import *
def game(width,height):
for x in range(width):
# fill a row in the list
squares.append([None] * height)
for y in range(height):
squares[-1][y] = Button(gameWindow)
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)
squares = []
gameWindow = Tk()
game(8,8)
# change color of square in x=3, y=4
squares[3][4].configure(bg='blue')
gameWindow.mainloop()
答案 1 :(得分:0)