Module ztcl::tui
ztcl::tui draws widgets of Tk in a grid of cells: a terminal twin of ztk. A script makes widgets with the commands of Tk in the namespace tui, places them with tui::pack, and binds scripts to their events. The design is in docs/dev/tui.md of the repository.
tui::configure -size {30 6}
tui::label .title -text "Backups" -style bold
tui::frame .box -borderwidth 1
tui::label .box.l -text "3 jobs, 1 running" -anchor w
tui::button .run -text Run -command {set ::ran 1}
tui::pack .title -side top
tui::pack .box -side top -fill both -expand 1
tui::pack .box.l -side top -fill x
tui::pack .run -side left -padx 1c
tui::send {Tab Return}
puts -nonewline [tui::snapshot]
puts "ran: $::ran"
Backups
╭────────────────────────────╮
│3 jobs, 1 running │
│ │
╰────────────────────────────╯
▶ Run ◀
ran: 1
The commands exist when the embedder turns on the capability tui, and they are absent otherwise; a child interp has them only when its parent has them. The capability reads the keys and writes the terminal of the process, so it is off by default (--allow tui turns it on for the ztcl program).
The first stage
This release has the core: the widgets frame, label, and button, the packer, bindings, the focus, and a test backend. The backend has no terminal: tui::update draws into a grid of cells, tui::snapshot gives the grid as text or as SVG, tui::send gives keys, and tui::click a press of the mouse. So a script and its tests give the same cells on each machine. The next stages add the real terminal, the widgets entry, listbox, scrollbar, and labelframe, grid, and tui::install, which makes the global names of Tk so a script of the common subset runs under ztk and in a terminal.
Commands
| Command | Description |
|---|---|
tui::frame PATH ?-option value ...? |
A container, with a box of lines with -borderwidth 1. |
tui::label PATH ?-option value ...? |
One line of text, from -text or -textvariable. |
tui::button PATH ?-option value ...? |
A button that runs -command at Return, space, or a click. |
tui::pack PATH ... ?-option value ...? |
Places widgets along a side of the free space of their parent, as the packer of Tk; forget, info, and slaves. |
tui::bind TAG ?SEQUENCE? ?SCRIPT? |
Binds a script to an event of a widget, a class, the toplevel ., or all. |
tui::focus ?PATH? |
Gives or sets the widget that takes the keys. |
tui::destroy ?PATH ...? |
Destroys widgets, with the widgets below them, their commands, and their bindings. |
tui::winfo OPTION PATH |
The place, the children, the class, and the state of a widget. |
tui::update |
Lays out and draws the widgets. |
tui::snapshot ?-text|-svg? ?-theme dark|light? |
The screen as text or as a picture. |
tui::send KEYS |
Gives keys, by their keysyms of Tk, to the widget with the focus. |
tui::click X Y ?BUTTON? |
Gives a press of a mouse button at a cell. |
tui::configure ?-size {COLS ROWS}? |
The size of the grid of the test backend, 80 by 24 at the start. |
Each widget also has a command with the name of its path: configure and cget, and invoke for a button. The command reference has each command with its options, examples, and errors.
Cells, not pixels
A size of a widget is in cells: -width and -height count columns and rows. A distance of Tk in pixels, such as -padx 8, becomes cells at 8 pixels for each column and 16 for each row, so a script of Tk keeps its proportions; a distance with c at the end is in cells. A color is a name or #rrggbb. The focused button shows ▶ TEXT ◀, so the focus shows with no color too.
Events and keys
An event goes to the bindings of its tags in the order of Tk: the path of the widget, its class, the toplevel ., and all. A script that ends with break stops the tags after it. The class of a button invokes it at Return, space, and a click, and all moves the focus with Tab and Shift-Tab. A binding gets the fields %W (the path), %K (the keysym), %A (the character), and %x and %y (the cell of a click in the widget).
tui::button .b -text B
tui::pack .b
tui::focus .b
tui::bind .b <Key> {puts "key %K on %W"}
tui::bind all <Control-q> {puts quit}
tui::send {x Control-q}
key x on .b
quit
Outside the subset
An option that only changes the look in Tk, such as -font, -relief, or -cursor, is accepted and has no effect. An option with no form in a terminal, such as -image, is an error that names it: tui: the option -image of label is not in the terminal subset, with the errorCode ZTCL TUI SUBSET -image. The aim is the scripts that a person writes for both front ends; a Tk app with images, a canvas, or libraries such as Tablelist runs under ztk.
Limits
An app has at most 10,000 widgets, a text of an option is at most 64 KB, and the grid is at most 1,000 cells on each side.