bec_widgets.utils.widget_io#

Attributes#

Classes#

CheckBoxHandler

Handler for QCheckBox widgets.

ComboBoxHandler

Handler for QComboBox widgets.

LabelHandler

Handler for QLabel widgets.

LineEditHandler

Handler for QLineEdit widgets.

SlideHandler

Handler for QCheckBox widgets.

SpinBoxHandler

Handler for QSpinBox and QDoubleSpinBox widgets.

TableWidgetHandler

Handler for QTableWidget widgets.

ToggleSwitchHandler

Handler for ToggleSwitch widgets.

WidgetHandler

Abstract base class for all widget handlers.

WidgetHierarchy

WidgetIO

Public interface for getting, setting values and connecting signals using handler mapping

WidgetTreeNode

Functions#

Module Contents#

class CheckBoxHandler#

Bases: WidgetHandler

Handler for QCheckBox widgets.

connect_change_signal(widget: qtpy.QtWidgets.QCheckBox, slot)#

Connect a change signal from this widget to the given slot. If the widget type doesn’t have a known “value changed” signal, do nothing.

slot: a function accepting two arguments (widget, value)

get_value(widget: qtpy.QtWidgets.QCheckBox, **kwargs)#

Retrieve value from the widget instance.

set_value(widget: qtpy.QtWidgets.QCheckBox, value)#

Set a value on the widget instance.

class ComboBoxHandler#

Bases: WidgetHandler

Handler for QComboBox widgets.

connect_change_signal(widget: qtpy.QtWidgets.QComboBox, slot)#

Connect a change signal from this widget to the given slot. If the widget type doesn’t have a known “value changed” signal, do nothing.

slot: a function accepting two arguments (widget, value)

get_value(widget: qtpy.QtWidgets.QComboBox, as_string: bool = False, **kwargs) int | str#

Retrieve value from the widget instance.

set_value(widget: qtpy.QtWidgets.QComboBox, value: int | str) None#

Set a value on the widget instance.

class LabelHandler#

Bases: WidgetHandler

Handler for QLabel widgets.

get_value(widget: qtpy.QtWidgets.QLabel, **kwargs)#

Retrieve value from the widget instance.

set_value(widget: qtpy.QtWidgets.QLabel, value)#

Set a value on the widget instance.

class LineEditHandler#

Bases: WidgetHandler

Handler for QLineEdit widgets.

connect_change_signal(widget: qtpy.QtWidgets.QLineEdit, slot)#

Connect a change signal from this widget to the given slot. If the widget type doesn’t have a known “value changed” signal, do nothing.

slot: a function accepting two arguments (widget, value)

get_value(widget: qtpy.QtWidgets.QLineEdit, **kwargs) str#

Retrieve value from the widget instance.

set_value(widget: qtpy.QtWidgets.QLineEdit, value: str) None#

Set a value on the widget instance.

class SlideHandler#

Bases: WidgetHandler

Handler for QCheckBox widgets.

connect_change_signal(widget: qtpy.QtWidgets.QSlider, slot)#

Connect a change signal from this widget to the given slot. If the widget type doesn’t have a known “value changed” signal, do nothing.

slot: a function accepting two arguments (widget, value)

get_value(widget: qtpy.QtWidgets.QSlider, **kwargs)#

Retrieve value from the widget instance.

set_value(widget: qtpy.QtWidgets.QSlider, value)#

Set a value on the widget instance.

class SpinBoxHandler#

Bases: WidgetHandler

Handler for QSpinBox and QDoubleSpinBox widgets.

connect_change_signal(widget: qtpy.QtWidgets.QSpinBox | qtpy.QtWidgets.QDoubleSpinBox, slot)#

Connect a change signal from this widget to the given slot. If the widget type doesn’t have a known “value changed” signal, do nothing.

slot: a function accepting two arguments (widget, value)

get_value(widget: qtpy.QtWidgets.QSpinBox | qtpy.QtWidgets.QDoubleSpinBox, **kwargs)#

Retrieve value from the widget instance.

set_value(widget: qtpy.QtWidgets.QSpinBox | qtpy.QtWidgets.QDoubleSpinBox, value)#

Set a value on the widget instance.

class TableWidgetHandler#

Bases: WidgetHandler

Handler for QTableWidget widgets.

connect_change_signal(widget: qtpy.QtWidgets.QTableWidget, slot)#

Connect a change signal from this widget to the given slot. If the widget type doesn’t have a known “value changed” signal, do nothing.

slot: a function accepting two arguments (widget, value)

get_value(widget: qtpy.QtWidgets.QTableWidget, **kwargs) list#

Retrieve value from the widget instance.

set_value(widget: qtpy.QtWidgets.QTableWidget, value) None#

Set a value on the widget instance.

class ToggleSwitchHandler#

Bases: WidgetHandler

Handler for ToggleSwitch widgets.

connect_change_signal(widget: bec_widgets.widgets.utility.toggle.toggle.ToggleSwitch, slot)#

Connect a change signal from this widget to the given slot. If the widget type doesn’t have a known “value changed” signal, do nothing.

slot: a function accepting two arguments (widget, value)

get_value(widget: bec_widgets.widgets.utility.toggle.toggle.ToggleSwitch, **kwargs)#

Retrieve value from the widget instance.

set_value(widget: bec_widgets.widgets.utility.toggle.toggle.ToggleSwitch, value)#

Set a value on the widget instance.

class WidgetHandler#

Bases: abc.ABC

Abstract base class for all widget handlers.

connect_change_signal(widget: qtpy.QtWidgets.QWidget, slot)#

Connect a change signal from this widget to the given slot. If the widget type doesn’t have a known “value changed” signal, do nothing.

slot: a function accepting two arguments (widget, value)

abstractmethod get_value(widget: qtpy.QtWidgets.QWidget, **kwargs)#

Retrieve value from the widget instance.

abstractmethod set_value(widget: qtpy.QtWidgets.QWidget, value)#

Set a value on the widget instance.

class WidgetHierarchy#
static _build_prefix(branch_flags: list[bool]) str#
static _filtered_children(widget: qtpy.QtWidgets.QWidget, exclude_internal_widgets: bool) list[qtpy.QtWidgets.QWidget]#
static _iter_widget_tree_nodes(widget: qtpy.QtWidgets.QWidget, parent: qtpy.QtWidgets.QWidget | None, exclude_internal_widgets: bool, visited: set[int], branch_flags: list[bool], depth: int)#
static export_config_to_dict(widget: qtpy.QtWidgets.QWidget, config: dict = None, indent: int = 0, grab_values: bool = False, print_hierarchy: bool = False, save_all: bool = True, exclude_internal_widgets: bool = True) dict#

Export the widget hierarchy to a dictionary.

Parameters:
  • widget – Widget to print the hierarchy of.

  • config (dict,optional) – Dictionary to export the hierarchy to.

  • indent (int,optional) – Level of indentation.

  • grab_values (bool,optional) – Whether to grab the values of the widgets.

  • print_hierarchy (bool,optional) – Whether to print the hierarchy to the console.

  • save_all (bool,optional) – Whether to save all widgets or only those with values.

  • exclude_internal_widgets (bool,optional) – Whether to exclude internal widgets (e.g. QComboBox in PyQt6).

Returns:

Dictionary containing the widget hierarchy.

Return type:

config(dict)

static find_ancestor(widget: qtpy.QtWidgets.QWidget | bec_widgets.utils.BECConnector, ancestor_class: Type[TAncestor] | str) TAncestor | None#

Find the closest ancestor of the specified class (or class-name string).

Parameters:
  • widget (QWidget) – The starting widget.

  • ancestor_class (Type[TAncestor] | str) – The ancestor class or class-name string to search for.

Returns:

The closest ancestor of the specified class, or None if not found.

Return type:

TAncestor | None

static get_bec_connectors_from_parent(widget) list#

Return all BECConnector instances whose closest BECConnector ancestor is the given widget, including the widget itself if it is a BECConnector.

static get_becwidget_ancestor(widget)#

Traverse up the parent chain to find the nearest BECConnector.

Parameters:

widget – Starting widget to find the ancestor for.

Returns:

The nearest ancestor that is a BECConnector, or None if not found.

static import_config_from_dict(widget, config: dict, set_values: bool = False) None#

Import the widget hierarchy from a dictionary.

Parameters:
  • widget – Widget to import the hierarchy to.

  • config

  • set_values

static iter_widget_tree(widget: qtpy.QtWidgets.QWidget, *, exclude_internal_widgets: bool = True)#

Yield WidgetTreeNode entries for the widget hierarchy.

static print_becconnector_hierarchy_from_app()#

Enumerate ALL BECConnector objects in the QApplication. Also detect if a widget is a PlotBase, and add any data items (PlotDataItem-like) that are also BECConnector objects.

Build a parent->children graph where each child’s ‘parent’ is its closest BECConnector ancestor. Print the entire hierarchy from the root(s).

The result is a single, consolidated tree for your entire running GUI, including PlotBase data items that are BECConnector.

static print_widget_hierarchy(widget, indent: int = 0, grab_values: bool = False, prefix: str = '', exclude_internal_widgets: bool = True, only_bec_widgets: bool = False, show_parent: bool = True) None#

Print the widget hierarchy to the console.

Parameters:
  • widget – Widget to print the hierarchy of.

  • indent (int, optional) – Level of indentation.

  • grab_values (bool,optional) – Whether to grab the values of the widgets.

  • prefix (str,optional) – Custom string prefix for indentation.

  • exclude_internal_widgets (bool,optional) – Whether to exclude internal widgets (e.g. QComboBox in PyQt6).

  • only_bec_widgets (bool, optional) – Whether to print only widgets that are instances of BECWidget.

  • show_parent (bool, optional) – Whether to display which BECWidget is the parent of each discovered BECWidget.

class WidgetIO#

Public interface for getting, setting values and connecting signals using handler mapping

static _find_handler(widget)#

Find the appropriate handler for the widget by checking its base classes.

Parameters:

widget – Widget instance.

Returns:

The handler class if found, otherwise None.

Return type:

handler_class

static check_and_adjust_limits(spin_box: qtpy.QtWidgets.QDoubleSpinBox, number: float)#

Check if the new limits are within the current limits, if not adjust the limits.

Parameters:

number (float) – The new value to check against the limits.

static connect_widget_change_signal(widget, slot)#

Connect the widget’s value-changed signal to a generic slot function (widget, value). This now delegates the logic to the widget’s handler.

static find_widgets(widget_class: qtpy.QtWidgets.QWidget | str, recursive: bool = True) list[qtpy.QtWidgets.QWidget]#

Return widgets matching the given class (or class-name string).

Parameters:
  • widget_class – Either a QWidget subclass or its class-name as a string.

  • recursive – If True (default), traverse all top-level widgets and their children; if False, scan app.allWidgets() for a flat list.

Returns:

List of QWidget instances matching the class or class-name.

static get_value(widget, ignore_errors=False, **kwargs)#

Retrieve value from the widget instance.

Parameters:
  • widget – Widget instance.

  • ignore_errors (bool, optional) – Whether to ignore if no handler is found.

static set_value(widget, value, ignore_errors=False)#

Set a value on the widget instance.

Parameters:
  • widget – Widget instance.

  • value – Value to set.

  • ignore_errors (bool, optional) – Whether to ignore if no handler is found.

class WidgetTreeNode#
depth: int#
parent: qtpy.QtWidgets.QWidget | None#
prefix: str#
widget: qtpy.QtWidgets.QWidget#
hierarchy_example()#
widget_io_signal_example()#
TAncestor#