Python module to allow learners to easily create GUIs
—
Container widgets are used to organize and group other widgets within your application. They provide layout management and visual grouping of related interface elements.
The Box widget is a general-purpose container for organizing other widgets. It can use automatic layout or grid-based positioning and provides visual grouping with optional borders.
class Box:
def __init__(self, master, layout="auto", grid=None, align=None,
visible=True, enabled=None, width=None, height=None,
border=None):
"""
Create a container widget for organizing other widgets.
Args:
master: Parent widget (App, Window, or another container)
layout (str): Layout manager ("auto" or "grid")
grid (list): Grid position [x, y] for grid layout
align (str): Alignment ("left", "right", "top", "bottom")
visible (bool): Initial visibility state
enabled (bool): Initial enabled state
width (int): Container width in pixels
height (int): Container height in pixels
border (int): Border width in pixels
"""
# Inherited properties from Container class
@property
def layout(self) -> str:
"""Get/set the layout manager ("auto" or "grid")."""
@layout.setter
def layout(self, value: str): ...
@property
def bg(self) -> str:
"""Get/set the background color."""
@bg.setter
def bg(self, value: str): ...
@property
def text_color(self) -> str:
"""Get/set the default text color for child widgets."""
@text_color.setter
def text_color(self, value: str): ...
@property
def text_size(self) -> int:
"""Get/set the default text size for child widgets."""
@text_size.setter
def text_size(self, value: int): ...
@property
def text_bold(self) -> bool:
"""Get/set default bold text for child widgets."""
@text_bold.setter
def text_bold(self, value: bool): ...
@property
def text_italic(self) -> bool:
"""Get/set default italic text for child widgets."""
@text_italic.setter
def text_italic(self, value: bool): ...from guizero import App, Box, Text, PushButton
app = App()
# Create a box container with a border
main_box = Box(app, border=2, width=300, height=200)
main_box.bg = "lightgray"
# Add widgets to the box
Text(main_box, text="This is inside the box")
PushButton(main_box, text="Button in box")
# Create a box with grid layout
grid_box = Box(app, layout="grid")
Text(grid_box, text="Top left", grid=[0, 0])
Text(grid_box, text="Top right", grid=[1, 0])
Text(grid_box, text="Bottom left", grid=[0, 1])
app.display()The TitleBox widget is a container with a visible title/label that groups related widgets under a descriptive heading.
class TitleBox:
def __init__(self, master, text="TitleBox", layout="auto", grid=None,
align=None, visible=True, enabled=None, width=None,
height=None, border=None):
"""
Create a titled container widget.
Args:
master: Parent widget (App, Window, or another container)
text (str): Title text displayed at the top
layout (str): Layout manager ("auto" or "grid")
grid (list): Grid position [x, y] for grid layout
align (str): Alignment ("left", "right", "top", "bottom")
visible (bool): Initial visibility state
enabled (bool): Initial enabled state
width (int): Container width in pixels
height (int): Container height in pixels
border (int): Border width in pixels
"""
@property
def text(self) -> str:
"""Get/set the title text."""
@text.setter
def text(self, value: str): ...
# Inherits all Container properties (layout, bg, text_color, etc.)from guizero import App, TitleBox, CheckBox, Slider
app = App()
# Create a titled section for settings
settings_box = TitleBox(app, text="Settings")
settings_box.bg = "white"
# Add settings widgets to the titled box
CheckBox(settings_box, text="Enable notifications")
CheckBox(settings_box, text="Auto-save")
Slider(settings_box, start=0, end=100)
# Create another titled section
display_box = TitleBox(app, text="Display Options")
CheckBox(display_box, text="Dark mode")
CheckBox(display_box, text="Show toolbar")
app.display()The ButtonGroup widget creates a group of mutually exclusive radio buttons, where only one option can be selected at a time.
class ButtonGroup:
def __init__(self, master, options=[], selected=None, horizontal=True,
command=None, grid=None, align=None, visible=True,
enabled=None, width=None, height=None):
"""
Create a group of mutually exclusive radio buttons.
Args:
master: Parent widget (App, Window, or container)
options (list): List of option texts/values
selected: Initially selected option
horizontal (bool): True for horizontal layout, False for vertical
command (function): Callback when selection changes
grid (list): Grid position [x, y] for grid layout
align (str): Alignment ("left", "right", "top", "bottom")
visible (bool): Initial visibility state
enabled (bool): Initial enabled state
width (int): Widget width in pixels
height (int): Widget height in pixels
"""
@property
def value(self):
"""Get/set the selected option value."""
@value.setter
def value(self, option): ...
@property
def value_text(self) -> str:
"""Get the text of the currently selected option."""
# Event handler
@property
def when_clicked(self):
"""Callback function when selection changes."""
@when_clicked.setter
def when_clicked(self, callback): ...from guizero import App, ButtonGroup, Text
app = App()
# Create a button group for size selection
size_group = ButtonGroup(
app,
options=["Small", "Medium", "Large"],
selected="Medium"
)
# Create a button group with horizontal layout
color_group = ButtonGroup(
app,
options=["Red", "Green", "Blue"],
horizontal=True
)
# Display current selections
selection_text = Text(app)
def update_selection():
selection_text.value = f"Size: {size_group.value}, Color: {color_group.value}"
size_group.when_clicked = update_selection
color_group.when_clicked = update_selection
# Initialize display
update_selection()
app.display()Containers support different layout approaches:
Default behavior where widgets are arranged automatically:
box = Box(app) # Uses auto layout by defaultExplicit positioning using grid coordinates:
box = Box(app, layout="grid")
Text(box, text="Widget 1", grid=[0, 0])
Text(box, text="Widget 2", grid=[1, 0])All containers support styling properties that can be inherited by child widgets:
container.bg = "lightblue" # Background color
container.text_color = "darkblue" # Default text color for children
container.text_size = 12 # Default text size for children
container.text_bold = True # Default bold text for children
container.text_italic = False # Default italic text for childrenContainers can be nested to create complex layouts:
from guizero import App, Box, TitleBox, Text, PushButton
app = App()
# Main container
main_box = Box(app, border=1)
# Left section
left_section = TitleBox(main_box, text="Controls")
PushButton(left_section, text="Start")
PushButton(left_section, text="Stop")
# Right section
right_section = TitleBox(main_box, text="Status")
Text(right_section, text="Ready")
app.display()Install with Tessl CLI
npx tessl i tessl/pypi-guizero