我制作了一款名为 Tic Tac Toe 的游戏,它是一款 2 人游戏,当你让他们连续或对角线获得 3 个 Xs 或 3 个 Os 时。
代码:
from guizero import *
empty = ' '
player = "X"
def clicked(z):
button = buttonlist[int(z)] # Finds out which button was pressed
global empty, player
if button.text != empty:
pass # If button already pushed do nothing
else:
# Marks button with user's go
button.text = player
# Switches players
if player == "X":
player = "O"
else:
player = "X"
return
app = App(title="Tic Tac Toe", layout="grid", width=200, height=200)
buttonlist = [] # Empty list to contain a list of Buttons
text_box = TextBox(app, text="enter username", align="top")
# Create Buttons for game, in 3 rows of 3
for y in range(3):
for x in range(3):
z = (3*y) + x
buttonlist.append(PushButton(app, text=empty,
args=str(z), grid=[y, x], command=clicked))
app.display()
我遇到的问题是当我输入线路时:
text_box = TextBox(app, text="enter username", align="top")
游戏仍然打开,但我收到一条错误消息:
*** GUIZERO WARNING *** [TextBox] object with text "enter username" will not be displayed because it has a missing grid reference.
所以有人可以帮我解决这个问题。