Topics covered
GUI - Graphical User Interface
guizero
widgets: App, PushButton, TextBox, Text
Adding text to a file
Tic-Tac-Toe game
GUIs
Python has a huge number of GUI frameworks (or toolkits) available for it, as you can see here: https://wiki.python.org/moin/GuiProgramming
Some are easier to use than others. To start you off I am going to introduce you to 'guizero'
Here is the website, where you can learn more about it and how to install the library, if it is not found in your edition of Python: https://lawsie.github.io/guizero
Guizero is a wrapper for the standard Tkinter library (included by default when Python3 is installed)
Let's get started with a simple "Hello World" program using guizero.
# app1.py
from guizero import *
app = App(title="Hello World")
app.display()
When run, it should produce a window like this:
It does not look like much but the App widget is the base on which you can place any of the other guizero widgets, such as: PushButtons,
Text (labels),
TextBoxes,
Comboboxes,
Sliders,
Checkboxes,
Menu bars and Pictures etc.
Here is how to place a button:
# but1.py
from guizero import *
def clicked():
app.title = 'You Clicked Me'
app = App(title="Hello World", layout="grid")
but1 = PushButton(app, grid=[0,0], text='Click Me', command=clicked)
app.display()
Note, PushButton references 'app' the name we gave to the base widget.
We have introduced another feature, layout in the App widget, which controls how the other widgets are to be placed on the App window, in this case the 'grid' layout.
The grid element specifies the row and column where the widget is to appear.
When the button is pushed or clicked, the program will branch to the 'clicked' function, defined above, and change the window title to "You Clicked Me!".
The TextBox widget allows a user to enter some text, here is an example:
# tbox0.py
from guizero import *
app = App(title="Hello World", layout="grid")
tbox1 = TextBox(app, grid=[0,0])
app.display()
Now let us combine a TextBox and a PushButton, here is an example:
# tboxbut2.py
from guizero import *
def clicked():
app.title = tbox1.get()
app = App(title="Hello World", layout="grid")
tbox1 = TextBox(app, grid=[0,0])
but1 = PushButton(app, grid=[0,1], text='Enter text in box and press me', command=clicked)
app.display()
Notice how the 'but1' widget is placed on the row below the TextBox using the 'grid=[0,1]' attribute.
The '0' was the column and '1' was the row.
Now let us change to text on a PushButton:
# pb1.py
from guizero import App, PushButton
def changebut():
if mybut.text == "X":
mybut.text = "O"
else:
mybut.text = "X"
app = App(height=200, width=200, layout="grid")
mybut = PushButton(app, grid=[0,0], command=changebut, text="O")
app.display()
This next example is the basis of a game of Tic-Tac-Toe (or Noughts and Crosses).
You need to add code to check if human player or computer has won.
See if you can add code to make the computer make its own intelligent choice for its next move.
Can you make the computer always win?
# TickTacToe.py from guizero import *
empty = ' '
player = "X"
def clicked(z):
button = buttonlist[int(z)] # Get the PushButton which was clicked
global empty, player
if button.text != empty:
pass # Do nothing if button already occupied
else:
# Mark button with user's symbol
button.text = player
# Now switch 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 PushButton objects
# Create nine PushButtons, in three rows of three
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()