【发布时间】:2018-01-15 23:50:38
【问题描述】:
我正在制作一个数独网格,并且我已经成功地生成了一个 9x9 的按钮网格。 我还创建了一个包含 81 个值的数组。 无论如何,我可以获取按钮内的值以匹配它们在数组中的相关索引。我只想显示几个数字,也许每行大约 3 个?有什么想法!?
这是按钮生成器:
#Create a 9x9 (rows x columns) grid of buttons inside the frame
for row_index in range(9):
for col_index in range(9):
if (row_index in {0, 1, 2, 6, 7, 8} and col_index in {3, 4, 5}) or \
(row_index in {3, 4, 5} and col_index in {0, 1, 2, 6, 7, 8}): #Colours a group of 3x3 buttons together to differentiate the board better.
colour = 'gray85'
else:
colour = 'snow'
c=True
btn = Button(frame, width = 12, height = 6, bg=colour) #create a button inside frame
btn.grid(row=row_index, column=col_index, sticky=N+S+E+W)
btn.bind("<Button-1>", LeftClick)
buttons.append(btn)
这是值数组:
easy = [
[8,5,1,9,4,3,6,7,2],
[4,3,9,6,7,2,5,1,8],
[6,7,2,1,8,5,9,3,4],
[1,2,3,7,9,4,8,6,5],
[7,6,5,2,1,8,4,9,3],
[9,4,8,3,5,6,7,2,1],
[5,9,6,4,2,1,3,8,7],
[2,8,7,5,3,9,1,4,6],
[3,1,4,8,6,7,2,5,9],
]
我玩过枚举的想法,但没有成功。
def Enumerate():
for row_index in enumerate(easy):
for col_index in enumerate(row_index):
for btn in buttons:
btn.config(text=col_index)
当我运行枚举函数时,会显示以下内容。 https://gyazo.com/1aeba588e321b5228e2d50d68ab24583
对于每个按钮的文本,它会输出数组中的最终列表。我觉得这与枚举周围的循环有关,但是我不确定我可以执行此任务的任何其他方式。
【问题讨论】:
标签: python arrays button tkinter sudoku