我开始学习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()
我对您的代码做了一些更改,使其更短、更优化。请参阅以下代码:
注:
StringVar()
在您的情况下,可以使用.config()
方法更改值。我将所有按钮实例都放在一个列表中,这样就可以通过程序中的索引访问它们李>printXO()
,它接受index
作为按钮中函数调用发送的参数李>lambda:printXO(<index>)
。这允许我们在需要时调用参数索引的函数,这里索引参数将对应于按钮,因此相同的关联可用于列表varo
和varx
以更改各自的值李>printXO()
中,我已将按钮状态更改为disabled
,以防止多次单击同一按钮。函数result()
在最后被调用,这样每次按下按钮后都可以进行结果检查李>result()
有一个条件来检查游戏中是否至少有5个动作(3乘x和2乘o(或)2乘x和3乘o)。此外,只有在游戏中完成所有9个动作后,draw
{希望有帮助
相关问题 更多 >
编程相关推荐