Widgets

Standard UI widgets for building interfaces.

Button

Defines the Button widget class.

A wrapper around ttk.Button with support for text variable binding, default button state, and user-defined callbacks.

Classes:
  • Button: A customizable button widget with optional label, default state, and event handling.

class mytk.button.Button(label='Button', default=False, width=None, user_event_callback=None, *args, **kwargs)[source]

Bases: Base

A custom button widget built on ttk.Button with label binding and event callback support.

Parameters:
  • label (str) – The initial text to display on the button.

  • default (bool) – Whether the button should be set as the default active button.

  • width (int, optional) – Width of the button in characters.

  • user_event_callback (callable, optional) – A function to call when the button is pressed.

property label

Gets or sets the current button label.

Returns:

The label text.

Return type:

str

create_widget(master, **kwargs)[source]

Creates the underlying ttk.Button widget.

Parameters:
  • master (tk.Widget) – The parent widget to attach this button to.

  • **kwargs – Additional keyword arguments.

action_callback()[source]

Invoke the user-supplied callback when the button is pressed.

property is_default

Gets or sets whether the button is marked as the active (default) button.

Returns:

True if the button is active, False otherwise.

Return type:

bool

Raises:

RuntimeError – If the widget has not yet been placed on screen.

set_as_default()[source]

Mark the button as the default active button.

Checkbox

checkbox.py — Checkbox widget for myTk UI framework.

This module defines a Checkbox class that wraps a ttk.Checkbutton widget, providing data binding via BooleanVar, user-defined callbacks, and convenient label configuration.

Classes:
  • Checkbox: A bindable checkbox with support for value access and change notifications.

Example

from mytk.checkbox import Checkbox

def on_toggle(checkbox):

print(“Checkbox state:”, checkbox.value)

box = Checkbox(label=”Accept terms”, user_callback=on_toggle)

class mytk.checkbox.Checkbox(*args, label='', user_callback=None, **kwargs)[source]

Bases: Base

A wrapper around ttk.Checkbutton that supports data binding and a user callback.

Provides: - BooleanVar synchronization - User-defined callback on state change - Label configuration via constructor

property value

Gets the current checked state of the checkbox.

Returns:

True if checked, False if unchecked.

Return type:

bool

create_widget(master, **kwargs)[source]

Creates the ttk.Checkbutton widget and binds its variable.

Parameters:
  • master (tk.Widget) – The parent widget into which this widget is placed.

  • **kwargs – Additional keyword arguments.

value_changed()[source]

Called when the checkbox value changes. Invokes the user callback if defined.

Raises:

RuntimeError – If the user callback raises an exception.

RadioButton

radiobutton.py — A myTk wrapper for ttk.Radiobutton with group linking and observation.

This module defines a RadioButton class that wraps a ttk.Radiobutton widget and provides a convenience method linked_group() to create a group of radio buttons that share a common value variable (IntVar), ensuring mutual exclusivity.

Key Features:

  • Radio buttons created via linked_group() share a value_variable, enabling coordinated selection.

  • Supports optional user callbacks triggered when selection changes.

  • Observes variable changes even when updated programmatically, not just on click.

Example:

def on_radio_change(button):
    print("Selected:", button.value)

radios = RadioButton.linked_group(
    {"Option A": 1, "Option B": 2, "Option C": 3},
    user_callback=on_radio_change,
)
class mytk.radiobutton.RadioButton(label, value, user_callback=None)[source]

Bases: Base

A bindable wrapper around ttk.Radiobutton.

Provides: - Optional user callback on selection change - Support for shared IntVar across multiple radio buttons - Observability for programmatic value changes

classmethod linked_group(labels_values, user_callback=None)[source]

Creates a group of mutually exclusive radio buttons that share the same value variable.

Parameters:
  • labels_values (dict[str, int]) – Dictionary mapping labels to integer values.

  • user_callback (Callable, optional) – Function called when any radio button is selected.

Returns:

List of configured radio buttons sharing the same IntVar.

Return type:

list[RadioButton]

create_widget(master, **kwargs)[source]

Creates the ttk.Radiobutton widget and binds it to the value variable.

Parameters:
  • master (tk.Widget) – The parent container.

  • **kwargs – Additional keyword arguments.

value_changed()[source]

Invoke the user-defined callback when the radio button is selected.

bind_variable(variable)[source]

Binds the radio button to a shared IntVar and sets up an observer for changes.

Notes

The command callback of a Radiobutton is only triggered by user clicks. To observe programmatic changes, this method also sets up observation.

Parameters:

variable (tk.IntVar) – The shared value variable for this radio group.

observed_property_changed(observed_object, observed_property_name, new_value, context)[source]

Handles updates to the shared variable even when changed programmatically.

Parameters:
  • observed_object – The object being observed (usually self).

  • observed_property_name (str) – Name of the property being observed.

  • new_value – The new value assigned.

  • context (str) – Context string used to identify the observer type.

Labels

class mytk.labels.Label(text=None, wrapping=False, **kwargs)[source]

Bases: Base

A text label widget wrapping ttk.Label with optional word wrapping.

create_widget(master)[source]

Create the underlying ttk.Label widget and bind its text variable.

set_label_wrap(event)[source]

Adjust the label wrap length to fit the current widget width.

class mytk.labels.URLLabel(url=None, text=None)[source]

Bases: Label

A clickable label that opens a URL in the default web browser.

create_widget(master)[source]

Create the label widget with underlined text and a click binding to open the URL.

open_url()[source]

Open the associated URL in the default web browser.

Entries

class mytk.entries.Entry(*args, value='', character_width=None, **kwargs)[source]

Bases: Base

A single-line text entry widget wrapping ttk.Entry.

create_widget(master)[source]

Create the ttk.Entry widget and bind its text variable.

property character_width

Get or set the entry width in characters.

property width

Get the entry width in pixels.

Raises:

NotImplementedError – If the widget has not been placed yet.

event_return_callback(event)[source]

Move focus to the parent widget when Return is pressed.

class mytk.entries.FormattedEntry(*args, value=0, character_width=None, format_string=None, reverse_regex=None, **kwargs)[source]

Bases: Base

A text entry that formats its numeric value using a format string and regex.

property value

Get or set the numeric value, applying the format string on change.

property character_width

Get or set the entry width in characters.

create_widget(master)[source]

Create the ttk.Entry widget with format and focus bindings.

event_return_callback(event)[source]

Move focus to the parent widget when Return is pressed.

event_focus_out(event)[source]

Parse the displayed text back into a numeric value on focus loss.

class mytk.entries.CellEntry(*args, tableview, item_id, column_name, user_event_callback=None, **kwargs)[source]

Bases: Base

An inline entry widget for editing a single cell in a TableView.

create_widget(master)[source]

Create the ttk.Entry widget pre-filled with the current cell value.

event_return_callback(event)[source]

Commit the edited value back to the data source on Return.

event_focusout_callback(event)[source]

Invoke the user callback and destroy the entry widget on focus out.

class mytk.entries.IntEntry(*args, value=0, width=None, minimum=0, maximum=100, increment=1, **kwargs)[source]

Bases: Base

An integer spinbox entry with configurable minimum, maximum, and increment.

create_widget(master)[source]

Create the ttk.Spinbox widget and bind its text variable.

property value

Get or set the integer value, clamped to the configured range.

property minimum

Get or set the minimum allowed value.

property maximum

Get or set the maximum allowed value.

property increment

Get or set the step increment for the spinbox.

mytk.entries.NumericEntry

alias of IntEntry

class mytk.entries.LabelledEntry(*args, label, text='', character_width=None, **kwargs)[source]

Bases: View

A composite widget combining a Label and an Entry side by side.

create_widget(master)[source]

Create the label and entry widgets arranged in a single row.

Slider

class mytk.controls.Slider(value=0, minimum=0, maximum=100, increment=1, width=200, height=20, orient='horizontal', delegate=None)[source]

Bases: Base

A horizontal or vertical slider control for selecting numeric values.

create_widget(master, **kwargs)[source]

Create the underlying ttk.Scale widget.

property value

The current numeric value of the slider.

property minimum

The minimum allowed value of the slider.

property maximum

The maximum allowed value of the slider.

observed_property_changed(observed_object, observed_property_name, new_value, context)[source]

Notify the delegate when the slider value changes.

PopupMenu

class mytk.popupmenu.PopupMenu(menu_items=None, user_callback=None)[source]

Bases: Base

A dropdown menu button that lets the user pick from a list of items.

property value

The currently selected menu item text.

create_widget(master)[source]

Create the underlying ttk.Menubutton and associated Menu.

clear_menu_items()[source]

Remove all items from the menu.

add_menu_items(menu_items)[source]

Add a list of string labels as selectable menu entries.

selection_changed(selected_index)[source]

Update the selection and invoke the user callback.

select_index(index)[source]

Programmatically select the menu item at the given index.