CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-guizero

Python module to allow learners to easily create GUIs

Pending
Overview
Eval results
Files

menus.mddocs/

Menu System

The MenuBar widget provides application-level menus for organizing commands and actions. It creates standard menu bars with dropdown menus containing menu items that can execute commands when selected.

Capabilities

Menu Bar

The MenuBar widget creates a menu bar that can be attached to application windows, providing standard menu functionality.

class MenuBar:
    def __init__(self, master, toplevel=[], options=[]):
        """
        Create a menu bar widget.
        
        Args:
            master (App): Parent application window
            toplevel (list): List of top-level menu names
            options (list): List of menu item configurations
        """

Menu Configuration

Menus are configured using a nested list structure that defines the menu hierarchy:

Basic Menu Structure

from guizero import App, MenuBar

app = App()

def file_new():
    print("New file")

def file_open():
    print("Open file")

def file_exit():
    app.destroy()

def help_about():
    print("About this application")

# Define menu structure
menubar = MenuBar(app,
    toplevel=["File", "Help"],
    options=[
        [
            ["New", file_new],
            ["Open", file_open],
            ["---"],  # Separator
            ["Exit", file_exit]
        ],
        [
            ["About", help_about]
        ]
    ]
)

app.display()

Menu Item Types

Menu items can be configured in several ways:

# Basic menu item with command
["Menu Text", callback_function]

# Menu separator
["---"]

# Menu with keyboard shortcut (display only)
["Copy\tCtrl+C", copy_function]

# Submenu (nested menu)
["Submenu", [
    ["Submenu Item 1", function1],
    ["Submenu Item 2", function2]
]]

Complete Menu Example

from guizero import App, MenuBar, TextBox, select_file, select_color, info

class TextEditor:
    def __init__(self):
        self.app = App(title="Simple Text Editor", width=600, height=400)
        
        # Text area
        self.text_area = TextBox(
            self.app, 
            multiline=True, 
            scrollbar=True,
            width="fill",
            height="fill"
        )
        
        # Create menu bar
        self.create_menus()
        
    def create_menus(self):
        self.menubar = MenuBar(self.app,
            toplevel=["File", "Edit", "Format", "Help"],
            options=[
                # File menu
                [
                    ["New\tCtrl+N", self.file_new],
                    ["Open\tCtrl+O", self.file_open],
                    ["Save\tCtrl+S", self.file_save],
                    ["Save As", self.file_save_as],
                    ["---"],
                    ["Exit\tCtrl+Q", self.file_exit]
                ],
                # Edit menu  
                [
                    ["Cut\tCtrl+X", self.edit_cut],
                    ["Copy\tCtrl+C", self.edit_copy],
                    ["Paste\tCtrl+V", self.edit_paste],
                    ["---"],
                    ["Select All\tCtrl+A", self.edit_select_all],
                    ["Clear", self.edit_clear]
                ],
                # Format menu
                [
                    ["Font Size", [
                        ["Small", lambda: self.set_font_size(10)],
                        ["Medium", lambda: self.set_font_size(12)],
                        ["Large", lambda: self.set_font_size(14)]
                    ]],
                    ["Text Color", self.format_color]
                ],
                # Help menu
                [
                    ["About", self.help_about],
                    ["Help", self.help_help]
                ]
            ]
        )
    
    # File menu methods
    def file_new(self):
        self.text_area.clear()
        
    def file_open(self):
        filepath = select_file(
            title="Open File",
            filetypes=[["Text files", "*.txt"], ["All files", "*.*"]],
            master=self.app
        )
        if filepath:
            try:
                with open(filepath, 'r') as f:
                    content = f.read()
                self.text_area.value = content
            except IOError as e:
                info("Error", f"Could not open file: {e}", self.app)
                
    def file_save(self):
        # Simplified save - in real app would track current file
        self.file_save_as()
        
    def file_save_as(self):
        filepath = select_file(
            title="Save File As",
            filetypes=[["Text files", "*.txt"], ["All files", "*.*"]],
            save=True,
            master=self.app
        )
        if filepath:
            try:
                with open(filepath, 'w') as f:
                    f.write(self.text_area.value)
                info("Success", "File saved successfully!", self.app)
            except IOError as e:
                info("Error", f"Could not save file: {e}", self.app)
                
    def file_exit(self):
        self.app.destroy()
    
    # Edit menu methods
    def edit_cut(self):
        # Note: Actual cut/copy/paste would require tkinter clipboard access
        print("Cut operation")
        
    def edit_copy(self):
        print("Copy operation")
        
    def edit_paste(self):
        print("Paste operation")
        
    def edit_select_all(self):
        print("Select all operation")
        
    def edit_clear(self):
        self.text_area.clear()
    
    # Format menu methods
    def set_font_size(self, size):
        self.text_area.text_size = size
        
    def format_color(self):
        color = select_color(master=self.app)
        if color:
            self.text_area.text_color = color
    
    # Help menu methods
    def help_about(self):
        info("About", "Simple Text Editor v1.0\nBuilt with guizero", self.app)
        
    def help_help(self):
        info("Help", "Use the File menu to open and save files.\nUse Edit menu for text operations.", self.app)
    
    def run(self):
        self.app.display()

# Create and run the application
editor = TextEditor()
editor.run()

Menu Best Practices

Menu Organization

Follow standard menu conventions:

# Standard menu order
toplevel=["File", "Edit", "View", "Tools", "Help"]

# Common File menu items
file_menu = [
    ["New", new_function],
    ["Open", open_function], 
    ["---"],
    ["Save", save_function],
    ["Save As", save_as_function],
    ["---"],
    ["Exit", exit_function]
]

Keyboard Shortcuts

Display keyboard shortcuts in menu text (display only - actual keyboard handling requires additional implementation):

menu_items = [
    ["New\tCtrl+N", new_function],
    ["Open\tCtrl+O", open_function],
    ["Save\tCtrl+S", save_function]
]

Menu Separators

Use separators to group related menu items:

menu_items = [
    ["New", new_function],
    ["Open", open_function],
    ["---"],  # Separator
    ["Save", save_function],
    ["Save As", save_as_function],
    ["---"],  # Separator  
    ["Exit", exit_function]
]

Submenus

Create submenus for related options:

format_menu = [
    ["Font", [
        ["Arial", lambda: set_font("Arial")],
        ["Times", lambda: set_font("Times")],
        ["Courier", lambda: set_font("Courier")]
    ]],
    ["Size", [
        ["10pt", lambda: set_size(10)],
        ["12pt", lambda: set_size(12)],
        ["14pt", lambda: set_size(14)]
    ]]
]

Menu Limitations

The guizero MenuBar has some limitations compared to full-featured menu systems:

  • No menu item states: Items cannot be enabled/disabled or checked/unchecked dynamically
  • No icons: Menu items cannot display icons
  • Limited styling: Menu appearance follows system defaults
  • Keyboard shortcuts: Display only - requires separate keyboard event handling for functionality

For applications requiring advanced menu features, consider using the underlying Tkinter Menu widget directly through the .tk property.

Platform Behavior

Menu behavior follows platform conventions:

  • Windows: Menu bar appears below title bar
  • macOS: Menu bar may appear at top of screen (system menu bar)
  • Linux: Menu bar appears below title bar, styling depends on desktop environment

Install with Tessl CLI

npx tessl i tessl/pypi-guizero

docs

application.md

containers.md

dialogs.md

display-widgets.md

index.md

input-widgets.md

menus.md

tile.json