【问题标题】:why tkinter message box didn't show up when i call them?为什么我打电话给他们时 tkinter 消息框没有出现?
【发布时间】:2020-12-25 18:25:36
【问题描述】:

我开始学习 python 并有兴趣使用 tkinter 使用 GUI 制作简单的井字游戏,但是当玩家达到目标时我遇到了一些问题,代码:

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

【问题讨论】:

  • 首先,请提供minimal, reproducible example,显示您的整个代码,了解如何输入 X 和 O。如果您使用键盘输入值,那么您可以尝试将按键事件与类似root.bind_all('<Key>', result) 的结果函数绑定,以便每次输入值时都会进行检查
  • @AST 我制作了 9 个按钮,当我单击时显示 X 和 O 文本,例如,我使用鼠标单击 button1 作为 X,然后我的 X1 值从 0 变为 1,所以当我在行中获得 3 个 X 时例如 X1、X5、X9,我将它们相加并给出一个值 3,所以获胜者是 X。但是没有任何反应,没有显示任何消息框。
  • 好吧,基本上所有 9 个按钮都会调用一个函数来更改按钮上的文本,这取决于点击它的玩家,对吧?一种方法是在这 9 个按钮调用的函数末尾添加一个函数调用 result(),以便每次单击任何按钮时都会进行获胜检查。正如我之前所说,请提供您的整个代码(或其中的合理部分),以便我可以通过引用来做出答案。
  • @AST 你能查一下吗,有完整的代码

标签: python user-interface tkinter


【解决方案1】:

我对您的代码进行了一些更改,使其更短且更优化。参考以下代码:

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)。此外,drawmessagebox 只有在游戏中完成所有 9 个动作后才会出现。

希望对您有所帮助!

【讨论】:

  • 谢谢兄弟,您的建议和更改。我可以通过有效的方式了解更多关于 python 编码的知识。
猜你喜欢
  • 2021-04-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-12
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
相关资源
最近更新 更多