【发布时间】:2018-06-17 09:50:10
【问题描述】:
我正在创建一个数独游戏,并且我生成了一系列按钮来创建一个 9x9 网格。每次单击按钮时,我希望它循环显示数字 1-9 的列表(因此,如果我希望按钮读取 6,则需要单击该按钮 6 次)。我已经设法实现了这一点,但是当我将它带到包含网格的主代码中时,它不起作用。
#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'
x = random.randint(1,9)
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)
def LeftClick(event, btn):
global position
btn.config(text=list1[position])
position=position+1
if position == len(list1):
position=0
btn.bind("<Button-1>", LeftClick)
知道为什么这不起作用吗?目前,当我单击按钮时没有任何反应。
【问题讨论】:
-
我不明白你的意思 - 我已经包含了我的示例代码。
-
复制/粘贴你的样本有什么作用吗?
-
不,样本没有任何作用——我说过。单击按钮时,它保持空白。不幸的是,我无法显示实际结果,因为我没有 gyazo 或类似的东西,并且无法在此处打印打印屏幕。对不起
-
抱歉,如果你没时间看minimal reproducible example,我没时间帮忙。
-
@JamesAnderson 示例做了某事,它引发了异常。
标签: python events button tkinter sudoku