Teek API Documentation

Inherits: Object

Instance Methods

add_package_path(path)

Add a directory to Tcl’s package search path.

Parameters
  • path String — directory containing Tcl packages

Returns void

after(ms, &block)

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

Parameters
  • ms Integer — delay in milliseconds

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

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(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

debugger [R]

Returns the value of attribute debugger.

interp [R]

Returns the value of attribute interp.

widgets [R]

Returns the value of attribute widgets.