为什么我给他们打电话时,他们的留言框没有出现?

2024-09-29 01:24:30 发布

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

我开始学习python,并对使用tkinter使用GUI制作简单的tic-tac-toe游戏感兴趣,但当玩家达到目标时,我遇到了一些问题,代码如下:

from tkinter import  *
import tkinter as tk
from tkinter import messagebox

#some code in here for button and other

if X9+X1+X5  == 3 or X1+X2+X3 == 3 or X4+X5+X6 == 3 or X7+X8+X9 == 3 or X7+X5+X3 == 3 or X1+X4+X7 == 3 or X2+X5+X8 == 3 or X3+X6+X9 == 3 :#X1-X9 variables for X coordinates
    messagebox.showinfo("X win !!!!", "Congrats X win") #win message X and goals

elif O1+O5+O9 == 3 or O1+O2+O3 == 3 or O4+O5+O6 == 3 or O7+O8+O9 == 3 or O7+O5+O3 == 3 or O1+O4+O7 == 3 or O2+O5+O8 == 3 or O3+O6+O9 == 3:#O1-O9 variables for O coordinates and goals
    messagebox.showinfo("O win !!!!", "Congrats O win")#win message for O
else :
    messagebox.showinfo("Draw", "Draw") #draw message

但是当我运行并获取X-X-X或O-O-O时,消息框没有出现,然后我按了一个按钮进行确认,它就工作了,但这不是我对这个程序的期望

def result():
    if X9+X1+X5  == 3 or X1+X2+X3 == 3 or X4+X5+X6 == 3 or X7+X8+X9 == 3 or X7+X5+X3 == 3 or X1+X4+X7 == 3 or X2+X5+X8 == 3 or X3+X6+X9 == 3 :
        messagebox.showinfo("X win !!!!", "Congrats X win")

    elif O1+O5+O9 == 3 or O1+O2+O3 == 3 or O4+O5+O6 == 3 or O7+O8+O9 == 3 or O7+O5+O3 == 3 or O1+O4+O7 == 3 or O2+O5+O8 == 3 or O3+O6+O9 == 3:
        messagebox.showinfo("O win !!!!", "Congrats O win")
    else :
        messagebox.showinfo("Draw", "Draw")
button10 = Button(top, text="Enter", width = 10, command = result ).place(x = 107, y = 300)

有人知道如何在不使用按钮呼叫的情况下显示消息吗

完整代码:

from tkinter import  *
import tkinter as tk
from tkinter import messagebox 

O1=X1=O2=X2=O3=X3=O4=X4=O5=X5=O6=X6=O7=X7=O8=X8=O9=X9=0
count = 0

def printXO1():
    global count
    global X1
    global O1
    count = count + 1
    if count % 2 == 0:
        button1.set("O")
        O1 = O1 + 1 
    else :
        button1.set("X")
        X1 = X1+1
    return count, X1, O1    
        

def printXO2():
    global count
    global X2
    global O2
    count = count + 1
    if count % 2 == 0:
        button2.set("O")
        O2 = O2 + 1
    else :
        button2.set("X")
        X2 = X2 + 1
    return count, X2, O2
    

def printXO3():
    global count
    global X3
    global O3
    count = count + 1
    if count % 2 == 0:  
        button3.set("O")
        O3 = O3 + 1
    else :
        button3.set("X")
        X3 = X3 +1
    return count, X3, O3

def printXO4():
    global count
    global X4
    global O4
    count = count + 1
    if count % 2 == 0:
        button4.set("O")
        O4 = O4 + 1 
    else :
        button4.set("X")
        X4 = X4 + 1
    return count, X2, O2

def printXO5():
    global count
    global X5
    global O5
    count = count + 1
    if count % 2 == 0:  
        button5.set("O")
        O5 = O5 + 1
    else :
        button5.set("X")
        X5 = X5 + 1
    return count, X5, O5
def printXO6():
    global count
    global X6
    global O6
    count = count + 1
    if count % 2 == 0:
        button6.set("O")
        O6 = O6 + 1
    else :
        button6.set("X")
        X6 = X6 + 1
    return count, X6, O6
def printXO7():
    global count
    global X7
    global O7
    count = count + 1
    if count % 2 == 0:
        button7.set("O")
        O7 = O7 + 1
    else :
        button7.set("X")
        X7 = X7 + 1
    return count, X7, O7


def printXO8():
    global count
    global X8
    global O8
    count = count + 1
    if count % 2 == 0:
        button8.set("O")
        O8 = O8 + 1
    else :
        button8.set("X")
        X8 = X8 + 1
    return count, X8, O8

def printXO9():
    global count
    global X9
    global O9
    count = count + 1
    if count % 2 == 0:
        button9.set("O") 
        O9 = O9 + 1
    else :
        button9.set("X")
        X9 = X9 + 1
    return count, X9, O9

top = tk.Tk()
top.geometry("300x350")

button1 = tk.StringVar()
button = tk.Button(top, textvariable = button1 , width = 10, height = 5, command =printXO1 ).place(x = 17, y = 10 )
button1.set(" ")

button2 = tk.StringVar()
button = Button(top, textvariable = button2 , width = 10, height = 5, command =printXO2 ).place(x = 107, y = 10 )
button2.set(" ")

button3 = tk.StringVar()
button = Button(top, textvariable = button3 , width = 10, height = 5, command =printXO3 ).place(x = 197, y = 10 )
button3.set(" ")

button4 = tk.StringVar()
button = Button(top, textvariable = button4 , width = 10, height = 5, command =printXO4 ).place(x = 17, y = 107 )
button4.set(" ")

button5 = tk.StringVar()
button = Button(top, textvariable = button5 , width = 10, height = 5, command =printXO5 ).place(x = 107, y = 107 )
button5.set(" ")

button6= tk.StringVar()
button = Button(top, textvariable = button6 , width = 10, height = 5, command =printXO6 ).place(x = 197, y = 107 )
button6.set(" ")

button7 = tk.StringVar()
button = Button(top, textvariable = button7 , width = 10, height = 5, command =printXO7 ).place(x = 17, y = 200 )
button7.set(" ")

button8 = tk.StringVar()
button = Button(top, textvariable = button8 , width = 10, height = 5, command =printXO8 ).place(x = 107, y = 200 )
button8.set(" ")

button9 = tk.StringVar()
button = Button(top, textvariable = button9 , width = 10, height = 5, command =printXO9 ).place(x = 197, y = 200 )
button9.set(" ")

def result():

    if X9+X1+X5  == 3 or X1+X2+X3 == 3 or X4+X5+X6 == 3 or X7+X8+X9 == 3 or X7+X5+X3 == 3 or X1+X4+X7 == 3 or X2+X5+X8 == 3 or X3+X6+X9 == 3 :
        messagebox.showinfo("X win !!!!", "Congrats X win")

    elif O1+O5+O9 == 3 or O1+O2+O3 == 3 or O4+O5+O6 == 3 or O7+O8+O9 == 3 or O7+O5+O3 == 3 or O1+O4+O7 == 3 or O2+O5+O8 == 3 or O3+O6+O9 == 3:
        messagebox.showinfo("O win !!!!", "Congrats O win")
    else :
        messagebox.showinfo("Draw", "Draw")
button10 = Button(top, text="Enter", width = 10, command = result ).place(x = 107, y = 300)

mainloop()

Tags: orcountglobalwinx1seto3x3
1条回答
网友
1楼 · 发布于 2024-09-29 01:24:30

我对您的代码做了一些更改,使其更短、更优化。请参阅以下代码:

from tkinter import  *
import tkinter as tk
from tkinter import messagebox 

def printXO(index):
    global count,varx,buttons,varo
    button=buttons[index]
    count+=1
    if count % 2 == 0:
        button.config(text="O",state='disabled')
        varo[index] =+ 1
    else :
        button.config(text="X",state='disabled')
        varx[index] =+ 1
    print(count)
    result()

def result():
    o_count=0
    for o in varo:
        o_count+=o
    x_count=0
    for x in varx:
        x_count+=x

    if (o_count>=2 and x_count>=3) or (o_count>=3 and x_count>=2):

        if varx[8]+varx[0]+varx[4]  == 3 or varx[0]+varx[1]+varx[2] == 3 or varx[3]+varx[4]+varx[5] == 3 or varx[6]+varx[7]+varx[8] == 3 or varx[6]+varx[4]+varx[2] == 3 or varx[0]+varx[3]+varx[6] == 3 or varx[1]+varx[4]+varx[7] == 3 or varx[2]+varx[5]+varx[8] == 3 :
            messagebox.showinfo("X win !!!!", "Congrats X win")

        elif varo[0]+varo[4]+varo[8] == 3 or varo[0]+varo[1]+varo[2] == 3 or varo[3]+varo[4]+varo[5] == 3 or varo[6]+varo[7]+varo[8] == 3 or varo[6]+varo[4]+varo[2] == 3 or varo[0]+varo[3]+varo[6] == 3 or varo[1]+varo[4]+varo[7] == 3 or varo[2]+varo[5]+varo[8] == 3:
            messagebox.showinfo("O win !!!!", "Congrats O win")
        elif count==9 :
            messagebox.showinfo("Draw", "Draw")

top = tk.Tk()
top.geometry("300x350")

button1 = tk.Button(top, width = 10, height = 5, command =lambda: printXO(0) )
button1.place(x = 17, y = 10 )
button2 = tk.Button(top, width = 10, height = 5, command =lambda: printXO(1) )
button2.place(x = 107, y = 10 )
button3 = tk.Button(top, width = 10, height = 5, command =lambda: printXO(2) )
button3.place(x = 197, y = 10 )
button4 = tk.Button(top, width = 10, height = 5, command =lambda: printXO(3) )
button4.place(x = 17, y = 107 )
button5 = tk.Button(top, width = 10, height = 5, command =lambda: printXO(4) )
button5.place(x = 107, y = 107 )
button6 = tk.Button(top, width = 10, height = 5, command =lambda: printXO(5) )
button6.place(x = 197, y = 107 )
button7 = tk.Button(top, width = 10, height = 5, command =lambda: printXO(6) )
button7.place(x = 17, y = 200 )
button8 = tk.Button(top, width = 10, height = 5, command =lambda: printXO(7) )
button8.place(x = 107, y = 200 )
button9 = tk.Button(top, width = 10, height = 5, command =lambda: printXO(8) )
button9.place(x = 197, y = 200 )

count = 0
buttons=[button1,button2,button3,button4,button5,button6,button7,button8,button9]
varx=[0, 0, 0, 0, 0, 0, 0, 0, 0]
varo=[0, 0, 0, 0, 0, 0, 0, 0, 0]

top.mainloop()

注:

  • 我已经初始化了2个包含x和o值的列表,这减少了变量的数量,现在可以使用索引了
  • 无需使用StringVar()在您的情况下,可以使用.config()方法更改值。我将所有按钮实例都放在一个列表中,这样就可以通过程序中的索引访问它们
  • 做了这些之后,就不需要那么多函数了,这里我创建了一个函数printXO(),它接受index作为按钮中函数调用发送的参数
  • 按钮的命令为lambda:printXO(<index>)。这允许我们在需要时调用参数索引的函数,这里索引参数将对应于按钮,因此相同的关联可用于列表varovarx以更改各自的值
  • 在函数printXO()中,我已将按钮状态更改为disabled,以防止多次单击同一按钮。函数result()在最后被调用,这样每次按下按钮后都可以进行结果检查
  • 最后,函数result()有一个条件来检查游戏中是否至少有5个动作(3乘x和2乘o(或)2乘x和3乘o)。此外,只有在游戏中完成所有9个动作后,draw{}才会出现

希望有帮助

相关问题 更多 >