井字游戏(Python,tkinter)。虫子在哪里?

2024-10-02 20:32:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我没有在代码中找到问题。在

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

如果你知道问题出在哪里或者如何正确地编写代码,请把它发给我!在


Tags: orandtextplaybuttontkfontheight
2条回答

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

有一些问题。首先,函数play接受一个参数buttons。此参数毫无意义,因为它立即被以下行覆盖:

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

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

^{pr2}$

所以我删除了覆盖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

{{cd11}的最后一行也被注意到了。所以我把它改为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并尽可能收集。查找错误并尝试根据互联网上已经存在的问题来解决它,而不是让其他无私的程序员免费为您解决问题。至少把已经存在的错误贴出来,这样我们一开始就知道你的问题是什么,并且清楚地知道你想要解决什么。以后,试着先用你自己的方式解决问题,因为下次你可能就没那么幸运了。在

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

相关问题 更多 >