gpt4 book ai didi

python - 文本框没有出现在我的游戏 Tic Tac Toe python 中

转载 作者:太空宇宙 更新时间:2023-11-04 01:52:16 26 4
gpt4 key购买 nike

我制作了一款名为 Tic Tac Toe 的游戏,这是一款 2 人游戏,当你让他们连续或对角地获得 3 个 X 或 3 个 O 时。

代码:

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.

有人可以帮我解决这个问题吗。

最佳答案

文档说在使用网格布局时需要传递网格位置: https://lawsie.github.io/guizero/layout/#grid-layout

这是您可以传递的参数的示例:

# this will display the textbox after the buttons
text_box = TextBox(app, text="enter username", align="top",grid=[0,4,3,1])
# 0 : column position (x)
# 4 : row position (y)
# 3 : span 3 columns (x span)
# 1 : span 1 row (y span)

如果你想在顶部显示2个文本框,你可以移动循环中的所有位置:

text_box = TextBox(app, text="enter username", align="top", grid=[0, 0, 3, 1])
text_box2 = TextBox(app, text="enter username2", align="top", grid=[0, 1, 3, 1])

z=0
for x in range(3):
for y in range(2, 5):
buttonlist.append(PushButton(app, text=empty, args=str(z), grid=[x, y], command=clicked))
z+=1

app.display()

关于python - 文本框没有出现在我的游戏 Tic Tac Toe python 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57736941/

26 4 0
文章推荐: python - 使用 Beautifulsoup 和选择器检索内容
文章推荐: c# - 最大宽度 CSS 属性不适用于 DIV
文章推荐: html -
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com