【问题标题】:Textbox not showing up in my game Tic Tac Toe python文本框没有出现在我的游戏井字游戏 python
【发布时间】:2019-08-31 09:52:29
【问题描述】:

我制作了一款名为 Tic Tac Toe 的游戏,它是一款 2 人游戏,当你让他们连续或对角获得 3 个 X 或 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.

那么请有人帮我解决这个问题。

【问题讨论】:

  • 很遗憾 guizero 似乎没有标签。我认为,这应该被标记为至少让合适的人参与其中的东西。看起来它是基于 tkinter 构建的——在这里添加标签是否正确?

标签: python python-3.x tkinter tic-tac-toe guizero


【解决方案1】:

文档说您在使用网格布局时需要传递网格位置: 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()

【讨论】:

  • 还有没有办法将井字游戏的网格向下移动?因为游戏现在不能正常运行
  • 当我点击第一个按钮时,它会标记它下面的那个,所以请你帮我修复它
  • 还有一个问题,有没有办法将网格向下移动一个,因为我想在第一个文本框下方添加另一个文本框
  • 我用第二个文本框和更简单的 z 计数器更新代码;)
  • 非常感谢,如果可能的话,您能否在其中添加标签以解释代码的作用,它使我更容易理解并帮助我记住以备将来使用
猜你喜欢
  • 2015-06-12
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-18
  • 2017-09-18
  • 2013-12-21
相关资源
最近更新 更多