【发布时间】:2021-11-23 19:59:30
【问题描述】:
我试图用这种方式在 python 中使用 tkinter 制作 XO 游戏,这是我的代码:
def button_function(name: tkinter.Button, n: str):
global row, column
info = name.grid_info()
row1 = info.get('row')
column1 = info.get('column')
name.grid_remove()
button = Button(root, text=n, font="decorative 9 bold", height=3, width=6, bg="white", state=DISABLED)
button.grid(row=row1, column=column1)
my_list = [cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9]
for x in my_list:
if x.grid_info():
info = x.grid_info()
row = info.get('row')
column = info.get('column')
x.grid_remove()
if n == "X":
my_list[my_list.index(x)] = Button(root, height=3, width=6, bg="gray",
command=lambda: button_function(x, "O"))
my_list[my_list.index(x)].grid(row=row, column=column)
elif n == "O":
my_list[my_list.index(x)] = Button(root, height=3, width=6, bg="gray",
command=lambda: button_function(x, "X"))
my_list[my_list.index(x)].grid(row=row, column=column)
else:
pass
info.clear()
def xo():
global cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9
cell1 = Button(root, height=3, width=6, bg="gray", command=lambda: button_function(cell1, "X"))
cell2 = Button(root, height=3, width=6, bg="gray", command=lambda: button_function(cell2, "X"))
cell3 = Button(root, height=3, width=6, bg="gray", command=lambda: button_function(cell3, "X"))
cell4 = Button(root, height=3, width=6, bg="gray", command=lambda: button_function(cell4, "X"))
cell5 = Button(root, height=3, width=6, bg="gray", command=lambda: button_function(cell5, "X"))
cell6 = Button(root, height=3, width=6, bg="gray", command=lambda: button_function(cell6, "X"))
cell7 = Button(root, height=3, width=6, bg="gray", command=lambda: button_function(cell7, "X"))
cell8 = Button(root, height=3, width=6, bg="gray", command=lambda: button_function(cell8, "X"))
cell9 = Button(root, height=3, width=6, bg="gray", command=lambda: button_function(cell9, "X"))
cell1.grid(row=2, column=1)
cell2.grid(row=2, column=3)
cell3.grid(row=2, column=5)
cell4.grid(row=4, column=1)
cell5.grid(row=4, column=3)
cell6.grid(row=4, column=5)
cell7.grid(row=6, column=1)
cell8.grid(row=6, column=3)
cell9.grid(row=6, column=5)
在第一个函数中,我试图在不更改单元格名称的情况下更改列表的内容,但我发现不能简单地通过向其添加值并将其网格化来完成,因为它将是本地的变量不属于列表。
x = Button(root, height=3, width=6, bg="gray", command=lambda: button_function(x, "O"))
所以我在上面尝试了这种方式,但我得到了这个错误
traceback (most recent call last):
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "D:\Python projects\python\Better XO.py", line 42, in <lambda>
cell8 = Button(root, height=3, width=6, bg="gray", command=lambda: button_function(cell8, "X"))
File "D:\Python projects\python\Better XO.py", line 23, in button_function
my_list[my_list.index(x)].grid(row=row, column=column)
ValueError: <tkinter.Button object .!button9> is not in list
那么有没有其他方法可以正确地做到这一点,谢谢
【问题讨论】:
-
请完整错误码
-
如果您发布完整的回溯,我们可以更轻松地查看错误所在..
-
@CoolCloud 正如你所说,我提供了所有的引用
-
@tdelaney 正如你所说,我提供了所有引用
-
@Matiiss 正如你所说,我提供了所有引用
标签: python list function tkinter valueerror