gpt4 book ai didi

python - Tkinter 和 Tic-Tac-Toe 的问题

转载 作者:行者123 更新时间:2023-12-01 08:36:41 25 4
gpt4 key购买 nike

我正在使用 Tkinter 编写 Tic-Tac-Toe 游戏。当一个用户获胜时,会出现一个带有“重新启动”按钮的顶级窗口,必须重新启动程序,但当我单击它时,我收到意外错误。我知道我的获胜者检查功能很愚蠢,但我可以用我目前的知识水平编写一个更好的功能。错误信息:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Eldiiar Raiymkulov\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:\Users\Eldiiar Raiymkulov\Desktop\xo2.py", line 62, in <lambda>
command=lambda x=x, y=y: clicked(y, x)))
File "C:\Users\Eldiiar Raiymkulov\Desktop\xo2.py", line 15, in clicked
isWinner(char)
File "C:\Users\Eldiiar Raiymkulov\Desktop\xo2.py", line 46, in isWinner
topMessage(char)
File "C:\Users\Eldiiar Raiymkulov\Desktop\xo2.py", line 30, in topMessage
topButton = Button(top, text="Restart", command=restart(root))
NameError: name 'root' is not defined

这是代码:

from tkinter import *

turn = True
btns = None
def clicked(y, x):
global turn, btns
char = ""
if turn:
char = "X"
else:
char = "O"

btns[y][x].config(text=char,
state=DISABLED)
isWinner(char)
turn = not turn

def restart(root):
global turn
turn = True
root.destroy()
main()
def topMessage(char):
global root
top = Toplevel()
top.title("Congratulations!")
topText = Label(top, text=f"{char} is a winner!")
topButton = Button(top, text="Restart", command=restart(root))
topText.grid(row=0)
topButton.grid(row=1)
def isWinner(char):
global root
#horizontal
if (((btns[1][1].cget("text") == char) and (btns[1][2].cget("text") == char) and (btns[1][3].cget("text") == char)) or
((btns[2][1].cget("text") == char) and (btns[2][2].cget("text") == char) and (btns[2][3].cget("text") == char)) or
((btns[3][1].cget("text") == char) and (btns[3][2].cget("text") == char) and (btns[3][3].cget("text") == char)) or
#vertical
((btns[1][1].cget("text") == char) and (btns[2][1].cget("text") == char) and (btns[3][1].cget("text") == char)) or
((btns[1][2].cget("text") == char) and (btns[2][2].cget("text") == char) and (btns[3][2].cget("text") == char)) or
((btns[1][3].cget("text") == char) and (btns[2][3].cget("text") == char) and (btns[3][3].cget("text") == char)) or
#diagonal
((btns[1][1].cget("text") == char) and (btns[2][2].cget("text") == char) and (btns[3][3].cget("text") == char)) or
((btns[1][3].cget("text") == char) and (btns[2][2].cget("text") == char) and (btns[3][1].cget("text") == char))):
topMessage(char)


def main():
global btns
root = Tk()
root.title("X and O of Daniar")
root.resizable(False, False)
btns = [None]
for y in range(1, 4):
row = [None]
for x in range(1, 4):
row.append(Button(root,
width=5,
height=3,
font="time 12 bold",
command=lambda x=x, y=y: clicked(y, x)))
row[x].grid(row=y, column=x)
btns.append(row)

Button(root,
text="Restart",
width=5,
command=lambda: restart(root)).grid(row=4, column=2)

root.mainloop()

main()

另外,如果您对 isWinner 函数有任何更明智的建议,可以与我分享吗?

最佳答案

这应该可以解决您的问题。

您需要在函数外部定义一个变量才能将其与全局一起使用,并让 main() 使用该变量。此外,如果您在命令中调用任何函数并将变量传递给该函数,请始终使用 lambda。

from tkinter import *

turn = True
btns = None
root = None


def clicked(y, x):
global turn, btns
char = ""
if turn:
char = "X"
else:
char = "O"

btns[y][x].config(text=char,
state=DISABLED)
isWinner(char)
turn = not turn


def restart(root):
global turn
turn = True
root.destroy()
main()


def topMessage(char):
global root
top = Toplevel()
top.title("Congratulations!")
topText = Label(top, text=f"{char} is a winner!")
topButton = Button(top, text="Restart", command=lambda: restart(root))
topText.grid(row=0)
topButton.grid(row=1)


def isWinner(char):
global root
# horizontal
if (((btns[1][1].cget("text") == char) and (btns[1][2].cget("text") == char) and (btns[1][3].cget("text") == char)) or
((btns[2][1].cget("text") == char) and (btns[2][2].cget("text") == char) and (btns[2][3].cget("text") == char)) or
((btns[3][1].cget("text") == char) and (btns[3][2].cget("text") == char) and (btns[3][3].cget("text") == char)) or
# vertical
((btns[1][1].cget("text") == char) and (btns[2][1].cget("text") == char) and (btns[3][1].cget("text") == char)) or
((btns[1][2].cget("text") == char) and (btns[2][2].cget("text") == char) and (btns[3][2].cget("text") == char)) or
((btns[1][3].cget("text") == char) and (btns[2][3].cget("text") == char) and (btns[3][3].cget("text") == char)) or
# diagonal
((btns[1][1].cget("text") == char) and (btns[2][2].cget("text") == char) and (btns[3][3].cget("text") == char)) or
((btns[1][3].cget("text") == char) and (btns[2][2].cget("text") == char) and (btns[3][1].cget("text") == char))):
topMessage(char)


def main():
global btns
global root
root = Tk()
root.title("X and O of Daniar")
root.resizable(False, False)
btns = [None]
for y in range(1, 4):
row = [None]
for x in range(1, 4):
row.append(Button(root,
width=5,
height=3,
font="time 12 bold",
command=lambda x=x, y=y: clicked(y, x)))
row[x].grid(row=y, column=x)
btns.append(row)

Button(root,
text="Restart",
width=5,
command=lambda: restart(root)).grid(row=4, column=2)

root.mainloop()


main()

关于python - Tkinter 和 Tic-Tac-Toe 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53684919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com