【发布时间】:2017-01-25 04:23:03
【问题描述】:
好吧,基本上我的导师已经为我设置了一项任务,即创建一个基于 GUI 的 Noughts and Crosses 不使用类的 Python 游戏,到目前为止,这就是我想出的。我的一个问题是我怎样才能做到这一点,所以每回合它都会将玩家切换到“X”,然后切换到下一个“O”,这样他们就可以轮流了,我尝试了很多方法,但我需要帮助哈哈。 谢谢!
代码如下:
player = "O"
from tkinter import *
game = Tk()
game.title("Noughts and Crosses")
game.geometry("")
app = Frame(game)
app.grid()
def tl():
topLeft.configure(text = player)
def tm():
topMid.configure(text = player)
def tr():
topRight.configure(text = player)
def ml():
midLeft.configure(text = player)
def mm():
midMid.configure(text = player)
def mr():
midRight.configure(text = player)
def bl():
botLeft.configure(text = player)
def bm():
botMid.configure(text = player)
def br():
botRight.configure(text = player)
#Top Row
topLeft = Button(app, text = "-", activebackground="red", command=tl)
topLeft.grid(row = 0, column = 0, ipadx=20, ipady=18, padx=10, pady=10)
topMid = Button(app, text = "-", activebackground="red", command=tm)
topMid.grid(row = 0, column = 1, ipadx=20, ipady=18, padx=10, pady=10)
topRight = Button(app, text = "-", activebackground="red", command=tr)
topRight.grid(row = 0, column = 2, ipadx=20, ipady=18, padx=10, pady=10)
#Middle Row
midLeft = Button(app, text = "-", activebackground="red", command=ml)
midLeft.grid(row = 1, column = 0, ipadx=20, ipady=18, padx=10, pady=10)
midMid = Button(app, text = "-", activebackground="red", command=mm)
midMid.grid(row = 1, column = 1, ipadx=20, ipady=18, padx=10, pady=10)
midRight = Button(app, text = "-", activebackground="red", command=mr)
midRight.grid(row = 1, column = 2, ipadx=20, ipady=18, padx=10, pady=10)
#Bottom Row
botLeft = Button(app, text = "-", activebackground="red", command=bl)
botLeft.grid(row = 2, column = 0, ipadx=20, ipady=18, padx=10, pady=10)
botMid = Button(app, text = "-", activebackground="red", command=bm)
botMid.grid(row = 2, column = 1, ipadx=20, ipady=18, padx=10, pady=10)
botRight = Button(app, text = "-", activebackground="red", command=br)
botRight.grid(row = 2, column = 2, ipadx=20, ipady=18, padx=10, pady=10)
game.mainloop()
【问题讨论】:
-
现在我正试图弄清楚如何做到这一点,所以当点击一次时,盒子会变成零,然后当玩家 2 的玩家点击它时会变成十字架
标签: python python-3.x tkinter