2017-04-04 85 views
-2
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錯誤井字遊戲的Python代碼

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

我怎樣才能解決這個問題?

+3

你從來沒有定義什麼文字就像是說 – Bobby

+0

使用支架上'text'錯誤。就像這個'buttons [「text」]' –

回答

1

你的錯誤是:

NameError: name 'text' is not defined.

檢查10號線的代碼。您使用buttons[text],但此處未定義text。這就是爲什麼你有這個錯誤。

只要將buttons[text]更改爲buttons["text"]並將所有text置於"text"。它會解決你的問題。

Editted代碼:

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() 

希望將作品:)

+0

如果你描述了你的改變,這個答案會好很多。否則讀者必須逐行比較原始代碼。 –

+0

感謝您的建議。我會編輯它。 –

+0

謝謝!它確實解決了錯誤@FarhadurRajaFahim但它現在在第33行顯示一個錯誤。語法錯誤,指向圓括號。 – iqra