Python 带有按钮的对应数组
我正在制作一个数独网格,我已经成功地生成了一个9x9的按钮网格。 我还创建了一个包含81个值的数组。 无论如何,我都可以得到按钮中的值,以匹配它们在数组中的相关索引。我只想显示几个数字,也许每行3个左右?有什么想法吗 这是按钮生成器:Python 带有按钮的对应数组,python,arrays,button,tkinter,sudoku,Python,Arrays,Button,Tkinter,Sudoku,我正在制作一个数独网格,我已经成功地生成了一个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
#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)
我一直在玩enumerate的想法,但是在这方面没有取得任何成功
def Enumerate():
for row_index in enumerate(easy):
for col_index in enumerate(row_index):
for btn in buttons:
btn.config(text=col_index)
当我运行enumerate函数时,将显示以下内容。
对于每个按钮的文本,它输出数组中的最终列表。我觉得这与枚举周围的循环有关,但我不确定是否还有其他方法可以执行此任务。在创建时将文本指定给按钮有意义吗?比如说,
button_text = str(easy[row_index][col_index])
btn = Button(frame, width = 12, height = 6, bg=colour, text=button_text)
枚举()函数存在以下问题:
def populate():
for row_index, row_data in enumerate(easy):
for col_index, cell_value in enumerate(row_data):
buttons[(row_index * 9) + col_index].config(text=cell_value)
这是有效的。然而,我打算增加难度等级。我如何使其根据所选的难度级别进行更改(请记住,难度级别是在顶部的菜单栏上选择的,因此当选择easy时,我希望生成这些数字),您可能会有其他嵌套列表,如hard,其中包含不同的数据。只需编写一个函数,使用与创建按钮时类似的循环范围,使用不同的列表获取按钮文本,这次只需配置按钮文本
按钮。configure(text=)
。我无法使用按钮。不过,请配置。在按钮生成器环路外无法识别。我以前有过这个问题。有什么建议吗?您将按钮定义为一个列表,我从buttons.append(btn)
中可以看到。您必须将按钮设置为全局或类变量。如果是类变量,那么同一类中的所有方法都将能够访问self.button。如果将其设置为全局,只需使用使用它的函数顶部的全局按钮
语句即可。
def populate():
for row_index, row_data in enumerate(easy):
for col_index, cell_value in enumerate(row_data):
buttons[(row_index * 9) + col_index].config(text=cell_value)