GUI zero (compared to tkinter)

Welcome to GUI zero, a new way to use tkinter

Why don’t we have another approach to GUI with python. Instead of using tkinter we could use GUI zero.

Install the module with

pip install guizero

Starting a window

It is very simple to start a window with guizero:

# Create a window with a title

from guizero import App
app = App(title="Hello world")
app.display()

As you can see, the syntax is easier than the one that you use with tkinter:

#
from tkinter import *

root = Tk()
root.title("Hello World!")

root.mainloop()

… but not so different. We have App() instead of Tk(), and display, instead of mainloop at the end.

Text and Buttons

What we call Label in tkinter, here is simply text, while Button is PushButton. As argument, we have the parent widget and then text and command as usual. The command argument takes the name of the function that will be called when the button is pushed. In this cas, when the button is pushed we go to the action function that will  change the text of textshow widget into “I know, you clicked”.

from guizero import App, Text, PushButton

# Method to call when button pressed

# Set up the app
app = App("First example of Gui Zero")


def action():
    textshow.value = "I know, you clicked!"


def line():
    Text(app, "--------------------")


text = Text(app, "Python GUI")

line()

button = PushButton(app, action, text="Click to start the action")

line()

textshow = Text(app, text="?", color="red")

line()

app.display()

Create a second window

from guizero import App, Window

app = App(title="Main window")
window = Window(app, title="2nd window")

app.display()

Opening and closing windows is easy

from guizero import App, Window

def open_window():
    window.show()

def close_window():
    window.hide()

app = App(title="Main window")

window = Window(app, title="2nd window")
window.hide()

open_button = PushButton(app, text="Open", command=open_window)
close_button = PushButton(window, text="Close", command=close_window)

app.display()

Open a window preventing from using the other

def open_window():
    window.show(wait=True)

Grid to position widgets

app = App(layout="grid")
text = Text(app, text="Hello world", grid=[0,1])

Example

App title

from guizero import App, PushButton
def do_nothing():
    print("Nothing happened")

app = App(title="Keypad example", width=100, height=90, layout="grid")
button1 = PushButton(app, command=do_nothing, text="1", grid=[0,0])
button2 = PushButton(app, command=do_nothing, text="2", grid=[1,0])
button3  = PushButton(app, command=do_nothing, text="3", grid=[2,0])
button4  = PushButton(app, command=do_nothing, text="4", grid=[0,1])
button5  = PushButton(app, command=do_nothing, text="5", grid=[1,1])
button6  = PushButton(app, command=do_nothing, text="6", grid=[2,1])
app.display()

Span Text

span text in two column with grid layout
from guizero import App, Text

app = App("Span text", layout="grid")

text = Text(app, text="column_x=0 row_y=0", bg="yellow", grid=[0,0,])
text2 = Text(app, text="column_x=1 row_y=0", bg="red", grid=[1,0])
text3 = Text(app, text="column_x=2 row_y=0", grid=[2,0])

app.display()

Span images

from guizero import App, Picture

app = App(title="guizero grid span example", width=460, height=210, layout="grid")
picture1 = Picture(app, image="std1.gif", grid=[0,0])
picture2 = Picture(app, image="std2.gif", grid=[1,0])
picture3 = Picture(app, image="tall1.gif", grid=[2,0,1,2])
picture4 = Picture(app, image="wide1.gif", grid=[0,1,2,1])

app.display()

Grid layout images

Warning box

Warning popup

from guizero import App, warn
app = App(title="Biscuit monitor")
warn("Uh oh!", "You are almost out of biscuits!")
app.display()

Yes no box

Yes No popup

from guizero import App, yesno, info, error
app = App(title="Snowman")
build_a_snowman = yesno("A question...", "Do you want to build a snowman?")
if build_a_snowman == True:
    info("Snowman", "It doesn't have to be a snowman")
else:
    error("Snowman", "Okay bye...")
app.display()

PushButton that pops info

from guizero import App, PushButton, info
app = App()
button = PushButton(app, command=info, args=["Info", "You pressed the button"])
app.display()

Do you really want to close?(.on_close method of App)

from guizero import App, Text, yesno

# Ask the user if they really want to close the window
def do_this_on_close():
    if yesno("Close", "Do you want to quit?"):
        app.destroy()

app = App()

title = Text(app, text="blank app")

# When the user tries to close the window, run the function do_this_on_close()
app.on_close(do_this_on_close)

app.display()

The rest of the documentation

This examples are based on the documentation of gui zero site. To read the rest of the documentation go here:

The size of the widgets

Colors

Images

Loops and sleeping

events

Using tkinter methods

Examples and resources

Recipes

App

Box

ButtonGroup

CheckBox

Combo

ListBox

MenuBar

Picture

PushButton

Slider

Text

TextBox

Waffle

Window

 

Tkinter test for students

Tkinter articles

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.