Tic Tac Toe游戏(Python,tkinter)。这个bug在哪里?

时间:2017-04-16 10:19:32

标签: python tkinter

我在代码中找不到问题。

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

如果您知道问题出在哪里或如何正确编写代码,请发送给我!

2 个答案:

答案 0 :(得分:0)

不要只复制代码@Abdelrahman。如果你这样做,你就不会从错误中吸取教训。

有一些问题。首先,函数play采用参数buttons。这个论点毫无意义,因为它被这一行立即覆盖:

buttons = [button1, button2, button3, button4, button5, button6, button7, button8, button9]

这意味着,不是buttons被设置为刚刚单击的按钮,而是所有按钮的列表。因此,当调用buttons["text"]时,您会得到TypeError,因为在需要整数时使用字符串作为索引。

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
  File "C:\Users\Genora51\Desktop\tictactoe.py", line 42, in <lambda>
    button1 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button1))
  File "C:\Users\Genora51\Desktop\tictactoe.py", line 13, in play
    if buttons["text"] == " " and click == True:
TypeError: list indices must be integers, not str

所以我删除了覆盖buttons的那一行,并将参数重命名为button以便清楚(它只是一个按钮)。长if - 语句仍然有效,因为按钮是全局变量。

def play(button):
    global click, tk

    if button["text"] == " " and click == True:
        button["text"] = "X"
        click = False
    elif button["text"] == " " and click == False:
        button['text'] = "O"
        click = False

我还注意到,在elif的最后一行,click仍被设置为False。所以我改变了它,将它设置为True,允许玩家在X-O-X-O之间交替,而不是X-O-O-O ......

elif (button1["text"] == "X"...

另一个问题是这是一个elif语句,所以它从未到达(第一个ifelif语句首先到达那里。那被改为if。现在,游戏机制在很大程度上起作用,并且代码在最后阶段得到了回应。但messagebox.askquestion没有做任何事情。所以我将代码的主体放在一个新函数start中,并添加了一些代码,这样当玩家选择时,它会关闭主窗口,如果他们想再玩一次,它就会会创建一个新窗口。

    answer = tkinter.messagebox.askquestion('X Player wins!!!', 'Do you want to play again')
    tk.destroy()
    if answer == 'yes': start()

我最后删除了一些额外的不必要代码(例如buttons = StringVar()),以产生这一点。

from tkinter import *
import tkinter.messagebox

click = True
tk = None
def start():
    global tk
    tk = Tk()
    tk.title("Tic Tac Toe")

    def play(button):
        global click, tk

        if button["text"] == " " and click:
            button["text"] = "X"
            click = False
        elif button["text"] == " ":
            button['text'] = "O"
            click = True

        if (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')
            tk.destroy()
            if answer == 'yes': start()

        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('O Player wins!!!', 'Do you want to play again')
            tk.destroy()
            if answer == 'yes': start()

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

start()

然而,请注意,我做这件事的唯一原因是因为我今天心情很好非常。下次遇到问题时,请务必阅读Traceback并尽可能收集。查找错误并尝试根据互联网上的内容解决问题,而不是要求其他利他主义程序员免费为您解决问题。至少发布已存在的错误,因此我们一开始就知道您的问题是什么,并明确您要解决的问题。将来,首先尝试以自己的方式解决问题,因为下次你可能不会那么幸运。

答案 1 :(得分:0)

from tkinter import *

import tkinter.messagebox

click = True

tk = None

def start():

    global tk

    tk = Tk()

    tk.title("Game-Tic Tac Toe:Two Player")



    def play(button):

        global click, tk



        if button["text"] == " " and click:

            button["text"] = "X"

            click = False

        elif button["text"] == " ":

            button['text'] = "O"

            click = True



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

            tk.destroy()

            if answer == 'yes': start()



        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('O Player wins!!!', 'Do you want to play again')

            tk.destroy()

            if answer == 'yes': start()



    button1 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button1))

    button1.grid(row=1, column=0)



    button2 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button2))

    button2.grid(row=1, column=1)



    button3 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button3))

    button3.grid(row=1, column=2)



    button4 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button4))

    button4.grid(row=2, column=0)



    button5 = Button(tk, text=" ", font=("Times 26 bold"), height=4, width=8, command=lambda:play(button5))

    button5.grid(row=2, column=1)



    button6 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button6))

    button6.grid(row=2, column=2)



    button7 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button7))

    button7.grid(row=3, column=0)



    button8 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button8))

    button8.grid(row=3, column=1)



    button9 = Button(tk, text=" ", font=('Times 26 bold'), height=4, width=8, command=lambda:play(button9))

    button9.grid(row=3, column=2)
    tk.mainloop()
start()