2

I have made a game called Tic Tac Toe it is a 2 player game and it is when you have etheir get 3 Xs or 3 Os in a row or diagonally.

Code:

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()

The problem that I am having is when I put in the line:

text_box = TextBox(app, text="enter username", align="top")

The game still opens up but I get an error saying:

*** GUIZERO WARNING *** [TextBox] object with text "enter username" will not be displayed because it has a missing grid reference.

So could some one please help me fix this.

  • It's unfortunate that guizero doesn't appear to have a tag. This should, I think, be tagged with something that at least gets the right people involved. It looks like it's built on tkinter - is it correct to add the tag here? – roganjosh Aug 31 '19 at 10:06

1 Answers1

2

The doc says you need to pass the grid position when you use a grid layout : https://lawsie.github.io/guizero/layout/#grid-layout

Here is an example of the parameter you can pass:

# 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)

If you want to display the 2 textbox on top, you can move all positions in the loop :

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()
PRMoureu
  • 12,817
  • 6
  • 38
  • 48
  • Also is there a way to shift the grid for the tic tac toe game down? because the game doesnt work properly now – needhelp786 Aug 31 '19 at 11:51
  • When I click the first button it marks the one below it instead so could you please help me fix it – needhelp786 Aug 31 '19 at 11:56
  • 1 more question is there a way to shift the grid down one more becuase i want to add another textbox beneath the first one – needhelp786 Aug 31 '19 at 12:13
  • i update the code with a second textbox, and a simpler counter for z ;) – PRMoureu Aug 31 '19 at 12:24
  • Thanks so much if possible could you put tags in to explain what the code does it makes it easier for me to understand and helps me to remember for future purposes – needhelp786 Aug 31 '19 at 12:31
  • what lines are mysterious for you ? – PRMoureu Aug 31 '19 at 12:38
  • I would say the counter part gets me a bit confused on what it does – needhelp786 Aug 31 '19 at 12:43
  • You just need a way to identify each button in the `buttonlist`, by index from 0 to 8. `z = (3*y) + x` was doing the same thing but it's a bit confusing since the position in the grid can move so it's easier to make a separate counter : `z=0` initializes `z` for the first iteration, then `z+=1` increments at the end. So the first button get index `0`, the second `1` and so on.. – PRMoureu Aug 31 '19 at 12:54
  • Thanks alot for your help – needhelp786 Aug 31 '19 at 17:54
  • Is there anyway to increase the size of the buttons? – needhelp786 Aug 31 '19 at 17:56
  • Could you create another question for that ? it's better if someone else is looking for the same issue. – PRMoureu Aug 31 '19 at 18:32
  • Its fine now i change my mind i want to keep them the same size – needhelp786 Aug 31 '19 at 18:47
  • If possible could you help me with another question regarding the same code above the link for it is [here](https://stackoverflow.com/questions/57744832/how-to-know-who-is-winner-in-tic-tac-toe-in-python-using-guizero) – needhelp786 Sep 01 '19 at 10:04