【发布时间】:2021-03-06 11:46:18
【问题描述】:
你好狂热的 python 用户...
我试图创建我的第一个 GUI,编写井字游戏程序,但我遇到了关于网格上 9 个按钮的问题。以下是生成按钮的部分代码:
button = 0
for x in range(3):
for y in range(3):
button = Button(root, text= " ", font=("Helvetica", 20), height=3, width=6, bg="SystemButtonFace", command=lambda button=button: b_click(button))
button.grid(row = x, column = y)
点击函数如下所示:
def b_click(b):
global clicked
if b["text"] == " " and clicked == True:
b["text"] = "X"
clicked = False
elif b["text"] == " " and clicked == False:
b["text"] = "O"
clicked = True
else:
messagebox.showerror("Tic Tac Toe", "Hey! That box has already been selected \nPick another box...")
我的问题是,每当我单击 GUI 上的一个按钮时,它都会选择并在我最初选择的那个按钮左侧的按钮上使用 b_click(b)...
我们将不胜感激...
【问题讨论】:
-
你知道第一个按钮的命令是
b_click(0),对吧? -
那么你会推荐什么?我觉得我一直在尝试一切来尝试解决这个问题......
标签: python tkinter button default-arguments