Skip to content

Tutorial Windows and Menus

rafageist edited this page Dec 1, 2026 · 1 revision

Tutorial step 2: windows and menus

Step 2 adds numbered windows, a Window menu, F6 to cycle through them, and a list you filter by typing. (Step 1 comes first.)

python examples/tutorial/step2_windows.py

Step 2: four windows, tiled, and a filtered list

Numbered windows, F6, Tile, and a filtered list

The program, step2_windows.py. It imports step 1's Hello and extends it:

# SPDX-License-Identifier: GPL-3.0-or-later
"""Step 2: windows. Numbered windows (Alt+1..9), F6 to cycle, Tile and Cascade, and a filtered list.

    python examples/tutorial/step2_windows.py
"""

from __future__ import annotations

from pathlib import Path

from textual.app import ComposeResult
from textual.widgets import Static
from textual_vision.constants import Command
from textual_vision.menus import Menu, MenuItem, Separator, SubMenu
from textual_vision.status_line import StatusDef, StatusItem

from step1_hello import Hello
from turboframe.ui.windows import LocationsWindow, MessageItem, TVWindow

# An application numbers its own commands from Command.USER.
CM_NEW = Command.USER + 1
CM_FILES = Command.USER + 2
CM_TILE = Command.USER + 3
CM_CASCADE = Command.USER + 4
CM_NEXT = Command.USER + 5


class NoteWindow(TVWindow):
    """A window is a frame (title, number, close and zoom boxes) around what compose_content yields."""

    def __init__(self, text: str, number: int) -> None:
        super().__init__(f"Note {number}", number=number)
        self.text = text

    def compose_content(self) -> ComposeResult:
        yield Static(self.text)


class Windows(Hello):
    TITLE = "Windows"

    def init_menu_bar(self) -> Menu:
        return Menu([
            SubMenu("~F~ile", MenuItem("~N~ew note", CM_NEW, "F4"), MenuItem("~F~iles...", CM_FILES, "F3"),
                    Separator(), MenuItem("E~x~it", Command.QUIT, "Alt+X")),
            SubMenu("~W~indow", MenuItem("~T~ile", CM_TILE), MenuItem("C~a~scade", CM_CASCADE),
                    MenuItem("~N~ext", CM_NEXT, "F6")),
        ])

    def init_status_line(self) -> list[StatusDef]:
        return [StatusDef(0, 0xFFFF, [StatusItem("~F4~ New", "f4", CM_NEW), StatusItem("~F3~ Files", "f3", CM_FILES),
                                      StatusItem("~F6~ Next", "f6", CM_NEXT), StatusItem("~Alt+X~ Exit", "alt+x", Command.QUIT)])]

    def start(self) -> None:
        self.new_note()
        self.new_note()

    def handle(self, command: int) -> bool:
        if command == CM_NEW:
            self.new_note()
        elif command == CM_FILES:
            self.show_files()
        elif command == CM_TILE:
            self.desktop.tile()
        elif command == CM_CASCADE:
            self.desktop.cascade()
        elif command == CM_NEXT:
            self.cycle_windows(forward=True)
        else:
            return super().handle(command)
        return True

    def new_note(self) -> None:
        number = self.free_number()  # 1 to 9: Alt+number brings the window up
        self.insert_window(NoteWindow(f"This is note {number}.\nAlt+{number} brings it to the front.", number))
        self.desktop.cascade()

    def show_files(self) -> None:
        """A list with a filter: type to narrow it, Enter on a line calls goto_location."""
        window = LocationsWindow("Files", number=self.free_number())
        self.insert_window(window)
        here = Path(__file__).parent
        self.call_after_refresh(window.show, [MessageItem(p.name, p, 0, 0) for p in sorted(here.glob("*.py"))])

    def goto_location(self, path, start, end) -> None:
        self.hint(f"chose {path.name}")


if __name__ == "__main__":
    Windows().run()

Windows

A window is a TVWindow (from turboframe.ui.windows): a Turbo Vision frame, with a title, a number, close and zoom boxes, and scroll bars on its border, around what its compose_content() yields. NoteWindow yields a Static; any Textual widget works.

  • insert_window(window) puts it on the desktop, on top.
  • free_number() gives the lowest number from 1 to 9 that no window has. With it, Alt+number brings that window to the front: DesktopApp binds Alt+1..9.
  • cycle_windows(forward=True) is Turbo Vision's F6: the bottom window comes up. With forward=False it is Shift+F6: the top one goes down.
  • desktop.tile() and desktop.cascade() arrange the windows; they are textual-vision's.
  • active_window() is the one on top; windows() lists them all, bottom to top.

A list you filter by typing

LocationsWindow is a window with a list of MessageItems. Each item is a line of text, optionally with a place: a path and a start and end offset. Typing filters it: the filter shows in the list's top line, » and what you typed, with the count. Esc clears the filter. Enter on an item with a place calls the application's goto_location(path, start, end), which you write. Here it only writes a hint; an editor would open the file there.

The same filter works on any list or tree: see Windows and Lists.

Commands of your own

CM_NEW = Command.USER + 1 and its neighbours are this step's commands. A MenuItem and a StatusItem both post the command, and handle() runs it. The menu shows F4, but F4 works because the status line binds it.

Next: Step 3: dialogs.

Clone this wiki locally