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:
1 2 3 4 5 |
# 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:
1 2 3 4 5 6 7 |
# 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”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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
1 2 3 4 5 6 |
from guizero import App, Window app = App(title="Main window") window = Window(app, title="2nd window") app.display() |
Opening and closing windows is easy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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
1 2 |
def open_window(): window.show(wait=True) |
Grid to position widgets
1 2 |
app = App(layout="grid") text = Text(app, text="Hello world", grid=[0,1]) |
Example
1 2 3 4 5 6 7 8 9 10 11 12 |
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
1 2 3 4 5 6 7 8 9 |
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
1 2 3 4 5 6 7 8 9 |
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() |
Warning box
1 2 3 4 |
from guizero import App, warn app = App(title="Biscuit monitor") warn("Uh oh!", "You are almost out of biscuits!") app.display() |
Yes no box
1 2 3 4 5 6 7 8 |
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
1 2 3 4 |
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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:
Tkinter test for students
Tkinter articles
