GUIZeroΒΆ

GUIZero is a Python module designed to produce simple and versatile user interfaces as a way to learning Python and graphical user interfaces (GUI).
##
# Fill waffle with random colours
#

from random import randint
from time import sleep

from guizero import App, Waffle

app = App(width=500, height=500)

# Create the waffle
size = 15
waf = Waffle(app, width=size, height=size)

# Loop filling random pixel with a random colour
while True:
    waf.set_pixel(randint(0, size - 1), randint(0, size - 1), (randint(0, 255), randint(0, 255), randint(0, 255)))
    app.update()
    sleep(0.01)
##
# Get key press and release
#

from time import sleep

from guizero import App, Text

app = App(width=500, height=500)


# Function called when key pressed
def key_pressed(event):
    text.value = event.key


# Function called when key released
def key_released(event):
    print(event.key)


# Add a window title
app.title = "Press any key"
# Create a text area
text = Text(app, size=50, color="magenta")
# Add function calls for evens
app.when_key_pressed = key_pressed
app.when_key_released = key_released

app.display()
##
# Quick start with input, button and keys
#

from guizero import App, Box, PushButton, Slider, Text, TextBox

app = App(layout="auto")


def button1_clicked():
    """
    Called when button clicked
    """

    slider1.value = 50


def slider1_moved(value):
    """
    Called when slider moved

    Args:
        value (str): String with slider position
    """

    box1.value = value
    text1.value = value


def key_pressed(event):
    """
    Function called when key pressed

    Args:
        event (event.EventData): Event object
    """

    print(event.key)
    box3.value = event.key


# Create text box for input
box1 = TextBox(app, width=50)

# Create a button to click
button1 = PushButton(app, command=button1_clicked)

# Create a slider
slider1 = Slider(app, command=slider1_moved, width=200)

# Create a text area to show key pressed
text1 = Text(app, size=50)
box2 = Box(app, border=True, width=100, height=100)
box3 = Text(box2, size=60, color="dark sea green")
app.when_key_pressed = key_pressed

app.display()
##
# Fill boxes with colour with mouse events
#

from random import choice

from guizero import App, Box, Window

app = App(layout="grid", width=200, height=200)

COLOURS = [
    "snow", "gainsboro", "floral white", "old lace", "linen", "antique white",
    "papaya whip", "blanched almond", "peach puff", "navajo white", "lemon chiffon",
    "mint cream", "azure", "alice blue", "lavender", "lavender blush", "misty rose",
    "dark slate gray", "slate gray", "light slate gray", "gray", "light grey", "midnight blue",
    "navy", "cornflower blue", "slate blue", "medium slate blue", "light slate blue",
    "medium blue", "royal blue", "blue", "dodger blue", "deep sky blue", "sky blue",
    "light sky blue", "steel blue", "light steel blue", "light blue", "powder blue",
    "pale turquoise", "dark turquoise", "medium turquoise", "turquoise", "cyan",
    "light cyan", "cadet blue", "medium aquamarine", "aquamarine", "dark green",
    "dark olive green", "dark sea green", "sea green", "medium sea green", "light sea green",
    "pale green", "spring green", "lawn green", "medium spring green", "green yellow",
    "lime green", "yellow green", "forest green", "olive drab", "dark khaki",
    "khaki", "pale goldenrod", "light goldenrod yellow", "light yellow", "yellow",
    "gold", "light goldenrod", "goldenrod", "dark goldenrod", "rosy brown", "indian red",
]


def fill_bg(event):
    """
    Called when mouse enters box

    Args:
        event (guizero.event.EventData): Event object
    """

    if str(event.widget.grid) == str([0, 0]):
        event.widget.bg = choice(COLOURS)
    elif str(event.widget.grid) == str([1, 0]):
        event.widget.bg = choice(COLOURS)
    elif str(event.widget.grid) == str([0, 1]):
        event.widget.bg = "green"
    elif str(event.widget.grid) == str([1, 1]):
        event.widget.bg = "#ff00ff"


def fill_cleared(event):
    """
    Called when mouse leaves box

    Args:
        event (guizero.event.EventData): Event object
    """

    event.widget.bg = None


def mouse_position(event):
    """
    Called when mouse clicked or dragged

    Args:
        event (guizero.event.EventData): Event object
    """

    print(
        f"mouse position absolute: {event.display_x}, {event.display_y}, mouse position relative: {event.x}, {event.y}    ",
        end="\r",
    )


def new_window(event):
    """
    Called when right mouse click

    Args:
        event (guizero.event.EventData): Event object
    """
    window = Window(app, title="2nd Window", height=200, width=100)


width = 100
height = 100
boxes = []
count = 0

for x in [0, 1]:
    for y in [0, 1]:
        # Create a list of boxes
        boxes.append(Box(app, width=width, height=height, grid=[x, y], border=1))

        # Add events
        boxes[count].when_mouse_enters = fill_bg
        boxes[count].when_mouse_leaves = fill_cleared
        boxes[count].when_clicked = mouse_position
        # boxes[count].when_left_button_pressed = say_hello
        boxes[count].when_right_button_pressed = new_window
        boxes[count].when_mouse_dragged = mouse_position
        count += 1

app.display()