我没有在代码中找到问题。在
from tkinter import *
import tkinter.messagebox
tk = Tk()
tk.title("Tic Tac Toe")
click = True
def play(buttons):
buttons = [button1, button2, button3, button4, button5, button6, button7, button8, button9]
global click
if buttons["text"] == " " and click == True:
buttons["text"] = "X"
click = False
elif buttons["text"] == " " and click == False:
buttons['text'] = "O"
click = False
elif (button1["text"] == "X" and button2["text"] == "X" and button3["text"] == "X" or
button4["text"] == "X" and button5["text"] == "X" and button6["text"] == "X" or
button7["text"] == "X" and button8["text"] == "X" and button9["text"] == "X" or
button1["text"] == "X" and button4["text"] == "X" and button7["text"] == "X" or
button2["text"] == "X" and button5["text"] == "X" and button8["text"] == "X" or
button3["text"] == "X" and button6["text"] == "X" and button9["text"] == "X" or
button1["text"] == "X" and button5["text"] == "X" and button9["text"] == "X" or
button3["text"] == "X" and button5["text"] == "X" and button7["text"] == "X"):
answer = tkinter.messagebox.askquestion('X Player wins!!!', 'Do you want to play again')
elif (button1["text"] == "O" and button2["text"] == "O" and button3["text"] == "O" or
button4["text"] == "O" and button5["text"] == "O" and button6["text"] == "O" or
button7["text"] == "O" and button8["text"] == "O" and button9["text"] == "O" or
button1["text"] == "O" and button4["text"] == "O" and button7["text"] == "O" or
button2["text"] == "O" and button5["text"] == "O" and button8["text"] == "O" or
button3["text"] == "O" and button6["text"] == "O" and button9["text"] == "O" or
button1["text"] == "O" and button5["text"] == "O" and button9["text"] == "O" or
button3["text"] == "O" and button5["text"] == "O" and button7["text"] == "O"):
answer = tkinter.messagebox.askquestion('X Player wins!!!', 'Do you want to play again')
buttons = StringVar()
button1 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button1))
button1.grid(row=1, column=0, sticky=S+N+E+W)
button2 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button2))
button2.grid(row=1, column=1, sticky=S+N+E+W)
button3 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button3))
button3.grid(row=1, column=2, sticky=S+N+E+W)
button4 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button4))
button4.grid(row=2, column=0, sticky=S+N+E+W)
button5 = Button(tk, text=" ", font=("Times 26 bold"), height=4, width=8, command=lambda:play(button5))
button5.grid(row=2, column=1, sticky=S+N+E+W)
button6 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button6))
button6.grid(row=2, column=2, sticky=S+N+E+W)
button7 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button7))
button7.grid(row=3, column=0, sticky=S+N+E+W)
button8 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button8))
button8.grid(row=3, column=1, sticky=S+N+E+W)
button9 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button9))
button9.grid(row=3, column=2, sticky=S+N+E+W)
tk.mainloop()
如果你知道问题出在哪里或者如何正确地编写代码,请把它发给我!在
不要只在末尾复制代码,@Abdelrahman。如果你这样做,你就不会从错误中吸取教训
有一些问题。首先,函数
play
接受一个参数buttons
。此参数毫无意义,因为它立即被以下行覆盖:这意味着
^{pr2}$buttons
不是设置为刚刚单击的按钮,而是所有按钮的列表。因此,当调用buttons["text"]
时,将得到一个TypeError
,用于在需要整数时使用字符串作为索引。在所以我删除了覆盖
buttons
的那一行,并将参数重命名为button
(它只会是一个按钮)。长的if
-语句仍然有效,因为按钮是全局变量。在{{cd11}的最后一行也被注意到了。所以我把它改为
True
,允许玩家在X-O-X-O之间交替,而不是X-O-O-O。。。在另一个问题是这是一个
elif
语句,因此它从未到达(第一个if
和elif
语句首先到达那里)。改为if
。现在,游戏机制基本上起作用了,代码在一个游戏结束时就开始了。但是messagebox.askquestion
什么也没做。所以我把代码的主体放在一个新函数start
中,并添加了一些代码,这样当玩家选择时,它会关闭主窗口,如果他们想再次播放,它会创建一个新窗口。在最后,我删除了一些多余的不必要的代码(例如
buttons = StringVar()
),以生成这个代码。在但是,请注意,我做这些的唯一原因是我今天心情很好。下一次遇到问题时,一定要阅读
Traceback
并尽可能收集。查找错误并尝试根据互联网上已经存在的问题来解决它,而不是让其他无私的程序员免费为您解决问题。至少把已经存在的错误贴出来,这样我们一开始就知道你的问题是什么,并且清楚地知道你想要解决什么。以后,试着先用你自己的方式解决问题,因为下次你可能就没那么幸运了。在相关问题 更多 >
编程相关推荐