Teek API Documentation

Inherits: Object

Instance Methods

add_debug_console(keybinding '<F12>')

Enable the Tk debug console. The console starts hidden and can be toggled with the given keyboard shortcut (default: F12).

The Tk console is a built-in interactive Tcl shell — useful for inspecting variables, running Tcl commands, and debugging widget layouts at runtime. It is available on macOS and Windows only; on Linux this method is a no-op (Linux has the real terminal).

Parameters
  • keybinding String — Tk event to toggle the console (default: "")

Returns Boolean — true if the console was created, false if unavailable on this platform

Example
app = Teek::App.new
app.add_debug_console            # F12 toggles console
app.add_debug_console("<F11>")   # custom key

add_package_path(path)

Add a directory to Tcl’s package search path.

Parameters
  • path String — directory containing Tcl packages

Returns void

after(ms, on_error: :raise, &block)

Schedule a one-shot timer. Calls the block after ms milliseconds.

Parameters
  • ms Integer — delay in milliseconds
  • on_error :raise, Proc, nil — error handling strategy: - :raise (default) — exception propagates to Tcl background error handler. - Proc — called with the exception; error is swallowed. - nil — error is silently swallowed.

Returns String — timer ID, pass to #after_cancel to cancel

@yield block to call when the timer fires

after_cancel(after_id)

Cancel a pending #after or #after_idle timer.

Parameters

Returns void

after_idle(&block)

Schedule a block to run once when the event loop is idle.

Returns String — timer ID, pass to #after_cancel to cancel

@yield block to call when the event loop is idle

appearance

Get the macOS window appearance. No-op (returns nil) on non-macOS.

Returns String, nil — "aqua", "darkaqua", "auto", or nil on non-macOS

Example
app.appearance          # => "aqua", "darkaqua", or "auto"
app.appearance = :light # force light mode
app.appearance = :dark  # force dark mode
app.appearance = :auto  # follow system setting
See also

appearance=(mode)

Set the macOS window appearance. No-op on non-macOS.

Parameters
  • mode Symbol, String:light, :dark, :auto, or a raw Tk value

Returns void

bind(widget, event, *subs, &block)

bool_to_tcl(val)

Convert a Ruby boolean to a Tcl boolean string (“1” or “0”).

Parameters
  • val Boolean

Returns String — "1" or "0"

busy(window: '.')

Show a busy cursor on a window while executing a block. The cursor is restored even if the block raises.

Parameters
  • window String — Tk window path

Returns — the block's return value

@yield the work to perform while busy

command(cmd, *args, **kwargs)

Build and evaluate a Tcl command from Ruby values. Positional args are converted: Symbols pass bare, Procs become callbacks, everything else is brace-quoted. Keyword args become -key value option pairs.

Parameters
  • cmd Symbol, String — the Tcl command name
  • args — positional arguments
  • kwargs — keyword arguments mapped to -key value pairs

Returns String — the Tcl result

Example
app.command(:pack, '.btn', side: :left, padx: 10)
# evaluates: pack .btn -side left -padx {10}

create_widget(type, path nil, parent: nil, **kwargs)

Create a Tk widget and return a Widget wrapper.

Auto-generates a unique path if none is given. The path is derived from the widget type and a monotonic counter.

Parameters
  • type String, Symbol — Tk widget command (e.g. 'ttk::button', :canvas)
  • path String, nil — explicit Tk path, or nil for auto-naming
  • parent Widget, String, nil — parent widget for path nesting
  • kwargs — keyword arguments passed to the Tk widget command

Returns Widget — the created widget

Examples
# Auto-named
btn = app.create_widget('ttk::button', text: 'Click')
# btn.path => ".ttkbtn1"
# Explicit path
frm = app.create_widget('ttk::frame', '.myframe')
# Nested under a parent
frm = app.create_widget('ttk::frame')
btn = app.create_widget('ttk::button', parent: frm, text: 'Click')
# btn.path => ".ttkfrm1.ttkbtn1"

dark?

Returns true if the window is currently displayed in dark mode. Always returns false on non-macOS.

Returns Boolean

destroy(widget '.')

Destroy a widget and all its children.

Parameters
  • widget String — Tk widget path (e.g. ".frame1")

Returns void

@raise ArgumentError

every(ms, on_error: :raise, &block)

Schedule a repeating timer. Calls the block every ms milliseconds until cancelled. The block runs on the main thread in the event loop, so it must be fast (don’t block the UI).

Parameters
  • ms Integer — interval in milliseconds
  • on_error :raise, Proc, nil — error handling strategy: - :raise (default) — cancels the timer and raises the exception from the next call to #update. - Proc — called with the exception; timer keeps running. - nil — cancels the timer silently; error stored in RepeatingTimer#last_error.

Returns RepeatingTimer — cancel handle

@yield block to call on each tick

Examples
# Basic polling loop
timer = app.every(50) { update_display }
timer.cancel  # stop later
# With error handler (timer keeps running)
timer = app.every(100, on_error: ->(e) { log(e) }) { risky_work }
# Silent cancel on error
timer = app.every(50, on_error: nil) { maybe_fails }
timer.last_error  # => check later

font_metrics(font)

Get font metrics (ascent, descent, linespace) for a given font. Uses Tk’s C font API directly.

Parameters
  • font String — font description (e.g. "Helvetica 12", "TkDefaultFont")

Returns Hash{Symbol => Integer}:ascent, :descent, :linespace

@raise Teek::TclError if the font is not found

get_variable(name)

Get a Tcl variable’s value.

Parameters
  • name String — variable name

Returns String — the value

@raise Teek::TclError if the variable doesn't exist

hide(window '.')

Hide a window without destroying it. Defaults to the root window (“.”).

Parameters
  • window String — Tk window path

Returns void

initialize(title: nil, track_widgets: true, debug: false, &block)

Returns App — a new instance of App

mainloop

Enter the Tk event loop. Blocks until the application exits.

Returns void

make_list(*args)

Build a properly-escaped Tcl list from Ruby strings.

Parameters
  • args Array<String> — elements to join

Returns String — a Tcl-formatted list

measure_chars(font, text, max_pixels, **opts)

Measure how many bytes of text fit within a pixel width limit. Useful for text truncation, ellipsis, and line wrapping.

Parameters
  • font String — font description (e.g. "Helvetica 12")
  • text String — text to measure
  • max_pixels Integer — maximum pixel width (-1 for unlimited)
  • opts Hash — options

Returns Hash{Symbol => Integer}:bytes and :width

Options
  • :partial_ok Boolean — allow partial character at boundary
  • :whole_words Boolean — break only at word boundaries
  • :at_least_one Boolean — always return at least one character

@raise Teek::TclError if the font is not found

package_names

List all packages known to this interpreter. Scans auto_path for package indexes before querying.

Returns Array<String>

package_present?(name)

Check if a package is already loaded in this interpreter.

Parameters
  • name String — package name

Returns Boolean

package_versions(name)

List available versions of a package. Scans auto_path for package indexes before querying.

Parameters
  • name String — package name

Returns Array<String>

register_callback(callable)

Register a Ruby callable as a Tcl callback. The callable can use throw for Tcl control flow: throw :teek_break - stop event propagation (like Tcl “break”) throw :teek_continue - Tcl TCL_CONTINUE throw :teek_return - Tcl TCL_RETURN

Parameters
  • callable #call — a Proc or lambda to invoke from Tcl

Returns Integer — callback ID, usable as ruby_callback <id> in Tcl

require_package(name, version nil)

Load a Tcl package into this interpreter.

Parameters
  • name String — package name (e.g. "BWidget")
  • version String, nil — minimum version constraint

Returns String — the version that was loaded

@raise Teek::TclError if the package is not found

set_variable(name, value)

Set a Tcl variable. Useful for widget textvariable and variable options.

Parameters
  • name String — variable name
  • value String — value to set

Returns String — the value

set_window_geometry(geometry, window: '.')

Set a window’s geometry (e.g. “400x300”, “400x30010050”).

Parameters
  • geometry String — geometry string
  • window String — Tk window path

Returns String — the geometry

set_window_resizable(width, height, window: '.')

Set whether a window is resizable.

Parameters
  • width Boolean — allow horizontal resize
  • height Boolean — allow vertical resize
  • window String — Tk window path

Returns void

set_window_title(title, window: '.')

Set a window’s title.

Parameters
  • title String — new title
  • window String — Tk window path

Returns String — the title

show(window '.')

Show a window. Defaults to the root window (“.”).

Parameters
  • window String — Tk window path

Returns void

split_list(str)

Split a Tcl list string into a Ruby array of strings.

Parameters
  • str String — a Tcl-formatted list

Returns Array<String>

tcl_eval(script)

Evaluate a raw Tcl script string and return the result. Prefer #command for building commands from Ruby values; use this when you need Tcl-level features like variable substitution or inline expressions that #command can’t express.

Parameters
  • script String — Tcl code to evaluate

Returns String — the Tcl result

tcl_invoke(*args)

Invoke a Tcl command with pre-split arguments (no Tcl parsing). Safer than #tcl_eval when arguments may contain special characters.

Parameters
  • args Array<String> — command name followed by arguments

Returns String — the Tcl result

tcl_to_bool(str)

Convert a Tcl boolean string (“0”, “1”, “yes”, “no”, etc.) to Ruby boolean.

Parameters
  • str String — a Tcl boolean value

Returns Boolean

text_width(font, text)

Measure the pixel width of a text string in a given font. Uses Tk’s C font API directly — faster than the Tcl font measure command.

Parameters
  • font String — font description (e.g. "Helvetica 12", "TkDefaultFont")
  • text String — text to measure

Returns Integer — pixel width

@raise Teek::TclError if the font is not found

unbind(widget, event)

Remove an event binding previously set with #bind.

Parameters
  • widget String — Tk widget path or class tag
  • event String — Tk event name, with or without angle brackets

Returns void

See also

unregister_callback(id)

Remove a previously registered callback by its ID.

Parameters

Returns void

update

Process all pending events and idle callbacks, then return.

Returns void

update_idletasks

Process only pending idle callbacks (e.g. geometry redraws), then return.

Returns void

window_geometry(window: '.')

Get a window’s current geometry.

Parameters
  • window String — Tk window path

Returns String — geometry string (e.g. "400x30000")

window_resizable(window: '.')

Get whether a window is resizable.

Parameters
  • window String — Tk window path

Returns Array(Boolean, Boolean) — [width_resizable, height_resizable]

window_title(window: '.')

Get a window’s current title.

Parameters
  • window String — Tk window path

Returns String — current title

Attributes

_pending_exception= [W]

@api private

debugger [R]

Returns the value of attribute debugger.

interp [R]

Returns the value of attribute interp.

widgets [R]

Returns the value of attribute widgets.