【问题标题】:Error in python code of tictactoe gametictactoe游戏的python代码错误
【发布时间】:2017-04-04 17:04:17
【问题描述】:
from tkinter import *
import tkinter.messagebox
tk=Tk()
tk.title("Tic Tac Toe")

click=True

def checker(buttons):
    global click
    if buttons[text]==" " and click==True:
        buttons[text]="X"
        click=False
    elif buttons[text]==" " and click==False:
        buttons[text]="O"
        click=True
    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 button5[text]=="X" and button9[text]=="X" or
        button3[text]=="X" and button5[text]=="X" and button7[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"):
        tkinter.messagebox.showinfo("Winner X:You won the game")

    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 button5[text]=="O" and button9[text]=="O" or
        button3[text]=="O" and button5[text]=="O" and button7[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"):
        tkinter.messagebox.showinfo("Winner O:You won the game")

buttons=StringVar()

button1=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button1))
button1.grid(row=0,column=0,sticky=S+N+E+W)

button2=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button2))
button2.grid(row=0,column=1,sticky=S+N+E+W)

button3=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button3))
button3.grid(row=0,column=2,sticky=S+N+E+W)

button4=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button4))
button4.grid(row=1,column=0,sticky=S+N+E+W)

button5=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button5))
button5.grid(row=1,column=1,sticky=S+N+E+W)

button6=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button6))
button6.grid(row=1,column=2,sticky=S+N+E+W)

button7=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button7))
button7.grid(row=2,column=0,sticky=S+N+E+W)

button8=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button8))
button8.grid(row=2,column=2,sticky=S+N+E+W)

tk.mainloop()

this is the screenshot of the name error which is diplayed on clicking a button

参考-https://www.skillshare.com/classes/Creating-a-TIC-TAC-TOE-game-using-Python-and-Tkinter/886857159

我该如何解决这个错误?

【问题讨论】:

  • 你永远不会像错误所说的那样定义什么文本
  • text 上使用方括号。像这样buttons["text"]

标签: python tkinter tic-tac-toe


【解决方案1】:

你的错误是:

NameError:名称“文本”未定义。

检查您的 10 号码行代码。您使用buttons[text],但这里text 之前没有定义。这就是您收到此错误的原因。

只需将buttons[text] 更改为buttons["text"] 并将所有text 放入"text"。它会解决你的问题。

修改后的代码:

from tkinter import *
import tkinter.messagebox
tk=Tk()
tk.title("Tic Tac Toe")

click=True

def checker(buttons):
    global click
    if buttons["text"]==" " and click==True:
        buttons["text"]="X"
        click=False
    elif buttons["text"]==" " and click==False:
        buttons["text"]="O"
        click=True
    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 button5["text"]=="X" and button9["text"]=="X" or
        button3["text"]=="X" and button5["text"]=="X" and button7["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"):
        tkinter.messagebox.showinfo("Winner X:You won the game")

    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 button5["text"]=="O" and button9["text"]=="O" or
        button3["text"]=="O" and button5["text"]=="O" and button7["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"):
        tkinter.messagebox.showinfo("Winner O:You won the game")

buttons=StringVar()

button1=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button1))
button1.grid(row=0,column=0,sticky=S+N+E+W)

button2=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button2))
button2.grid(row=0,column=1,sticky=S+N+E+W)

button3=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button3))
button3.grid(row=0,column=2,sticky=S+N+E+W)

button4=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button4))
button4.grid(row=1,column=0,sticky=S+N+E+W)

button5=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button5))
button5.grid(row=1,column=1,sticky=S+N+E+W)

button6=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button6))
button6.grid(row=1,column=2,sticky=S+N+E+W)

button7=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button7))
button7.grid(row=2,column=0,sticky=S+N+E+W)

button8=Button(tk,text=" ",font=('Times 26 bold'),height=4,width=8,command=lambda:checker(button8))
button8.grid(row=2,column=2,sticky=S+N+E+W)

tk.mainloop()

希望它会起作用:)

【讨论】:

  • 如果您描述了您所做的更改,这个答案会好很多。否则读者必须逐行与原始代码进行比较。
  • 谢谢!它确实解决了@FarhadurRajaFahim 的错误,但它现在在第 33 行显示错误。语法错误,指向圆括号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-14
  • 2015-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多