【发布时间】: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