Package m45wxcontrols

Custom control wrappers for wxPython by M45Development

Sub-modules

m45wxcontrols.accessible_spin

Accessible spinbutton class

m45wxcontrols.custom_text_entry

Text entry dialog with custom button labels.

m45wxcontrols.universal_list

Universal list class to choose between ListCtrl and DataViewListCtrl

Classes

class AccessibleSpinCtrl (parent, label_text, initial_val, min_val, max_val, inc)
Expand source code
class AccessibleSpinCtrl(wx.BoxSizer):
    """
    Accessible floating-point spin control composed of wx widgets.

    The control exposes a labeled text field as the primary interaction point
    and keeps a wx.SpinButton in sync for mouse users. Arrow keys on the text
    field adjust the value directly, which makes the widget easier to use with
    screen readers.
    """

    def __init__(self, parent, label_text, initial_val, min_val, max_val, inc):
        """
        Initialize accessible spin control.

        Args:
            parent: Parent wx window that owns the child controls.
            label_text: Label shown next to the text field and used as its
                accessible name.
            initial_val: Initial numeric value displayed in the control.
            min_val: Minimum allowed value.
            max_val: Maximum allowed value.
            inc: Increment used by the spin button and Up/Down arrow keys.
        """
        super().__init__(wx.HORIZONTAL)
        
        self.min_val = min_val
        self.max_val = max_val
        self.inc = inc
        
        # 1. The Label
        self.label = wx.StaticText(parent, label=label_text)
        self.Add(self.label, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 5)
        
        # 2. The TextCtrl (The primary interaction point)
        self.text_ctrl = wx.TextCtrl(parent, value=f"{initial_val:.1f}")
        self.text_ctrl.SetName(label_text)
        self.Add(self.text_ctrl, 1, wx.EXPAND | wx.ALL, 5)
        
        # 3. The SpinButton (Visible for mouse users)
        self.spin_btn = wx.SpinButton(parent, style=wx.SP_VERTICAL)
        self.spin_btn.SetRange(int(min_val / inc), int(max_val / inc))
        self.spin_btn.SetValue(int(initial_val / inc))
        self.spin_btn.SetCanFocus(False)
        self.Add(self.spin_btn, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 5)
        
        # Bindings
        self.spin_btn.Bind(wx.EVT_SPIN, self.on_spin)
        self.text_ctrl.Bind(wx.EVT_TEXT, self.on_text_entry)
        
        # Accessibility: Allow Up/Down arrow keys directly in the TextCtrl
        self.text_ctrl.Bind(wx.EVT_KEY_DOWN, self.on_key_down)

    def Bind(self, event_type, handler, *args, **kwargs):
        """
        Proxy method to allow the dialog to bind to changes.
        We map any binding attempt to our internal controls.

        Args:
            event_type: Ignored wx event binder supplied by the caller.
            handler: Callable to bind to text and spin changes.
            *args: Additional positional arguments forwarded to wx.Bind.
            **kwargs: Additional keyword arguments forwarded to wx.Bind.
        """
        # We bind to both text changes and spin changes
        self.text_ctrl.Bind(wx.EVT_TEXT, handler, *args, **kwargs)
        self.spin_btn.Bind(wx.EVT_SPIN, handler, *args, **kwargs)

    def _adjust_value(self, steps):
        """
        Internal helper to increment/decrement the value.

        Args:
            steps: Number of increments to apply. Negative values decrement.
        """
        try:
            current = float(self.text_ctrl.GetValue())
            new_val = current + (steps * self.inc)
            # Clamp value
            new_val = max(self.min_val, min(self.max_val, new_val))
            
            # Update both controls
            formatted_val = f"{new_val:.1f}"
            self.text_ctrl.SetValue(formatted_val) 
            # Note: SetValue triggers NVDA to read the new content
            
            self.spin_btn.SetValue(int(new_val / self.inc))
        except ValueError:
            pass

    def on_spin(self, event):
        """
        Handle spin button changes and mirror the value into the text field.

        Args:
            event: wx spin event containing the current spin position.
        """
        new_val = event.GetPosition() * self.inc
        self.text_ctrl.ChangeValue(f"{new_val:.1f}")
        event.Skip() # CRITICAL: Allows the event to propagate to the dialog

    def on_text_entry(self, event):
        """
        Handle manual text edits and keep the spin button position in sync.

        Args:
            event: wx text event emitted by the internal text control.
        """
        try:
            val = float(self.text_ctrl.GetValue())
            self.spin_btn.SetValue(int(val / self.inc))
        except ValueError:
            pass
        event.Skip() # CRITICAL: Allows the event to propagate to the dialog

    def on_key_down(self, event):
        """
        Handle keyboard increments from the internal text control.

        Args:
            event: wx key event. Up and Down adjust the value; all other keys
                are passed through.
        """
        key = event.GetKeyCode()
        if key in (wx.WXK_UP, wx.WXK_DOWN):
            steps = 1 if key == wx.WXK_UP else -1
            self._adjust_value(steps)
            
            # Manually fire a text event so the dialog knows something changed
            # since _adjust_value uses SetValue/ChangeValue
            cmd_event = wx.CommandEvent(wx.wxEVT_TEXT, self.text_ctrl.GetId())
            cmd_event.SetString(self.text_ctrl.GetValue())
            self.text_ctrl.GetEventHandler().ProcessEvent(cmd_event)
        else:
            event.Skip()

    def GetValue(self):
        """
        Return the current numeric value.

        Returns:
            float: Parsed text control value, or 2.0 if the text is not a
            valid number.
        """
        try:
            return float(self.text_ctrl.GetValue())
        except ValueError:
            return 2.0

Accessible floating-point spin control composed of wx widgets.

The control exposes a labeled text field as the primary interaction point and keeps a wx.SpinButton in sync for mouse users. Arrow keys on the text field adjust the value directly, which makes the widget easier to use with screen readers.

Initialize accessible spin control.

Args

parent
Parent wx window that owns the child controls.
label_text
Label shown next to the text field and used as its accessible name.
initial_val
Initial numeric value displayed in the control.
min_val
Minimum allowed value.
max_val
Maximum allowed value.
inc
Increment used by the spin button and Up/Down arrow keys.

Ancestors

  • wx._core.BoxSizer
  • wx._core.Sizer
  • wx._core.Object
  • sip.wrapper
  • sip.simplewrapper

Methods

def Bind(self, event_type, handler, *args, **kwargs)
Expand source code
def Bind(self, event_type, handler, *args, **kwargs):
    """
    Proxy method to allow the dialog to bind to changes.
    We map any binding attempt to our internal controls.

    Args:
        event_type: Ignored wx event binder supplied by the caller.
        handler: Callable to bind to text and spin changes.
        *args: Additional positional arguments forwarded to wx.Bind.
        **kwargs: Additional keyword arguments forwarded to wx.Bind.
    """
    # We bind to both text changes and spin changes
    self.text_ctrl.Bind(wx.EVT_TEXT, handler, *args, **kwargs)
    self.spin_btn.Bind(wx.EVT_SPIN, handler, *args, **kwargs)

Proxy method to allow the dialog to bind to changes. We map any binding attempt to our internal controls.

Args

event_type
Ignored wx event binder supplied by the caller.
handler
Callable to bind to text and spin changes.
*args
Additional positional arguments forwarded to wx.Bind.
**kwargs
Additional keyword arguments forwarded to wx.Bind.
def GetValue(self)
Expand source code
def GetValue(self):
    """
    Return the current numeric value.

    Returns:
        float: Parsed text control value, or 2.0 if the text is not a
        valid number.
    """
    try:
        return float(self.text_ctrl.GetValue())
    except ValueError:
        return 2.0

Return the current numeric value.

Returns

float
Parsed text control value, or 2.0 if the text is not a

valid number.

def on_key_down(self, event)
Expand source code
def on_key_down(self, event):
    """
    Handle keyboard increments from the internal text control.

    Args:
        event: wx key event. Up and Down adjust the value; all other keys
            are passed through.
    """
    key = event.GetKeyCode()
    if key in (wx.WXK_UP, wx.WXK_DOWN):
        steps = 1 if key == wx.WXK_UP else -1
        self._adjust_value(steps)
        
        # Manually fire a text event so the dialog knows something changed
        # since _adjust_value uses SetValue/ChangeValue
        cmd_event = wx.CommandEvent(wx.wxEVT_TEXT, self.text_ctrl.GetId())
        cmd_event.SetString(self.text_ctrl.GetValue())
        self.text_ctrl.GetEventHandler().ProcessEvent(cmd_event)
    else:
        event.Skip()

Handle keyboard increments from the internal text control.

Args

event
wx key event. Up and Down adjust the value; all other keys are passed through.
def on_spin(self, event)
Expand source code
def on_spin(self, event):
    """
    Handle spin button changes and mirror the value into the text field.

    Args:
        event: wx spin event containing the current spin position.
    """
    new_val = event.GetPosition() * self.inc
    self.text_ctrl.ChangeValue(f"{new_val:.1f}")
    event.Skip() # CRITICAL: Allows the event to propagate to the dialog

Handle spin button changes and mirror the value into the text field.

Args

event
wx spin event containing the current spin position.
def on_text_entry(self, event)
Expand source code
def on_text_entry(self, event):
    """
    Handle manual text edits and keep the spin button position in sync.

    Args:
        event: wx text event emitted by the internal text control.
    """
    try:
        val = float(self.text_ctrl.GetValue())
        self.spin_btn.SetValue(int(val / self.inc))
    except ValueError:
        pass
    event.Skip() # CRITICAL: Allows the event to propagate to the dialog

Handle manual text edits and keep the spin button position in sync.

Args

event
wx text event emitted by the internal text control.
class CustomTextEntryDialog (parent, message, caption, default_value='', ok_label='&OK', cancel_label='&Cancel')
Expand source code
class CustomTextEntryDialog(wx.Dialog):
    """
    Text entry dialog with custom button labels.

    The dialog mirrors the simple wx.TextEntryDialog workflow while allowing
    callers to provide custom OK and Cancel labels.
    """

    def __init__(self, parent, message, caption, default_value="", ok_label="&OK", cancel_label="&Cancel"):
        """
        Initialize custom text entry dialog.

        Args:
            parent: Parent window (MainFrame)
            message: text input message
            caption: text input caption
            default_value: optional default value
            ok_label: optional OK button label
            cancel_label: optional cancel button label
        """
        super().__init__(parent, title=caption)

        sizer = wx.BoxSizer(wx.VERTICAL)

        # Message
        text = wx.StaticText(self, label=message)
        sizer.Add(text, 0, wx.ALL, 10)

        # Text input
        self.text_ctrl = wx.TextCtrl(self, value=default_value)
        sizer.Add(self.text_ctrl, 0, wx.EXPAND | wx.ALL, 10)

        # Buttons
        button_sizer = wx.BoxSizer(wx.HORIZONTAL)
        ok_button = wx.Button(self, wx.ID_OK, ok_label)
        cancel_button = wx.Button(self, wx.ID_CANCEL, cancel_label)

        ok_button.SetDefault()  # OK as default button

        button_sizer.Add(ok_button, 0, wx.ALL, 5)
        button_sizer.Add(cancel_button, 0, wx.ALL, 5)

        sizer.Add(button_sizer, 0, wx.ALIGN_RIGHT | wx.ALL, 10)

        self.SetSizer(sizer)
        self.Fit()
        self.Center()

        self.text_ctrl.SetFocus()

    def GetValue(self):
        """
        Return value from custom text entry dialog.

        Returns:
            str: Current text entered by the user.
        """
        return self.text_ctrl.GetValue()

Text entry dialog with custom button labels.

The dialog mirrors the simple wx.TextEntryDialog workflow while allowing callers to provide custom OK and Cancel labels.

Initialize custom text entry dialog.

Args

parent
Parent window (MainFrame)
message
text input message
caption
text input caption
default_value
optional default value
ok_label
optional OK button label
cancel_label
optional cancel button label

Ancestors

  • wx._core.Dialog
  • wx._core.TopLevelWindow
  • wx._core.NonOwnedWindow
  • wx._core.Window
  • wx._core.WindowBase
  • wx._core.EvtHandler
  • wx._core.Object
  • wx._core.Trackable
  • sip.wrapper
  • sip.simplewrapper

Methods

def GetValue(self)
Expand source code
def GetValue(self):
    """
    Return value from custom text entry dialog.

    Returns:
        str: Current text entered by the user.
    """
    return self.text_ctrl.GetValue()

Return value from custom text entry dialog.

Returns

str
Current text entered by the user.
class UniversalListCtrl (parent,
size=wx.Size(-1, -1),
style=134217760,
checkboxes=False,
force_dataview=False)
Expand source code
class UniversalListCtrl:
    """
    Cross-platform list wrapper with optional checkbox support.

    wx.ListCtrl is used on Windows, while wx.dataview.DataViewListCtrl is used
    on Linux and macOS for better accessibility. The wrapper normalizes the
    small subset of list operations used by the application.
    """

    EVT_ITEM_CHECKED = wx.NewEventType()

    def __init__(
        self,
        parent,
        size=wx.DefaultSize,
        style=wx.LC_REPORT | wx.BORDER_SUNKEN,
        checkboxes=False,
        force_dataview=False
    ):
        """
        Initialize universal list control.

        Args:
            parent: Parent wx window.
            size: Initial control size.
            style: wx style flags applied to the underlying list control.
            checkboxes: Whether rows should support checked/unchecked state.
            force_dataview: Force DataViewListCtrl even on platforms that would
                normally use wx.ListCtrl.
        """
        # We use DataViewListCtrl for Linux and macOS unless forced (better accessibility)
        self.use_dataview = sys.platform.startswith('linux') or sys.platform == 'darwin' or force_dataview
        self.checkboxes = checkboxes
        self._checkbox_column = None

        if self.use_dataview:
            self.control = dv.DataViewListCtrl(parent, style=style, size=size)
        else:
            self.control = wx.ListCtrl(parent, style=style, size=size)
            if self.checkboxes:
                self.control.EnableCheckBoxes()

    def InsertColumn(self, col, heading, width=wx.LIST_AUTOSIZE, checkbox=False):
        """
        Insert a column in the underlying control.

        Args:
            col: Zero-based column index.
            heading: Text shown in the column header.
            width: Column width or wx autosize constant.
            checkbox: Whether this column stores checkbox/toggle values.
        """
        if self.use_dataview:
            if checkbox:
                self.control.AppendToggleColumn(
                    heading,
                    mode=dv.DATAVIEW_CELL_ACTIVATABLE,
                    width=width
                )
                self._checkbox_column = col
            else:
                self.control.AppendTextColumn(heading, width=width)
        else:
            self.control.InsertColumn(col, heading, width=width)
            if checkbox:
                self._checkbox_column = col

    def Append(self, entry):
        """
        Adds a row to the list.
        'entry' must be a list or tuple of values matching the column count.

        Args:
            entry: Sequence of row values. Checkbox columns should contain
                truthy or falsy values; text columns are converted to strings.
        """
        if self.use_dataview:
            # DataViewListCtrl.AppendItem expects exactly one argument: a sequence
            # Keep toggle columns as bool and format text columns as strings.
            formatted_entry = [
                bool(item) if col_idx == self._checkbox_column else str(item)
                for col_idx, item in enumerate(entry)
            ]
            self.control.AppendItem(formatted_entry)
        else:
            # ListCtrl: Insert the first item, then set sub-items
            index = self.control.GetItemCount()
            if self._checkbox_column == 0:
                self.control.InsertItem(index, "")
                self.control.CheckItem(index, bool(entry[0]))
                text_start_col = 1
            else:
                self.control.InsertItem(index, str(entry[0]))
                text_start_col = 1
            for col_idx in range(text_start_col, len(entry)):
                self.control.SetItem(index, col_idx, str(entry[col_idx]))

    def Bind(self, event_type, handler):
        """
        Unifies binding for common list events.

        Args:
            event_type: wx event binder or UniversalListCtrl event type.
            handler: Callable that receives the normalized event.
        """
        if event_type == wx.EVT_LIST_ITEM_SELECTED:
            if self.use_dataview:
                # Map DataView selection to List selection logic
                self.control.Bind(dv.EVT_DATAVIEW_SELECTION_CHANGED, 
                                  lambda evt: self._handle_selection(evt, handler))
            else:
                self.control.Bind(wx.EVT_LIST_ITEM_SELECTED, handler)
        
        elif event_type in [wx.EVT_CONTEXT_MENU, wx.EVT_CHAR_HOOK]:
            # These are standard wx.Window events, no mapping needed
            self.control.Bind(event_type, handler)

        elif event_type == self.EVT_ITEM_CHECKED:
            if self.use_dataview:
                self.control.Bind(
                    dv.EVT_DATAVIEW_ITEM_VALUE_CHANGED,
                    lambda evt: self._handle_check(evt, handler)
                )
            else:
                self.control.Bind(
                    wx.EVT_LIST_ITEM_CHECKED,
                    lambda evt: self._handle_check(evt, handler)
                )
                self.control.Bind(
                    wx.EVT_LIST_ITEM_UNCHECKED,
                    lambda evt: self._handle_check(evt, handler)
                )
        
        else:
            # Fallback for other events
            self.control.Bind(event_type, handler)

    def _handle_selection(self, evt, user_handler):
        """
        Internal helper to normalize DataViewEvent so it feels 
        closer to a ListEvent for the handler.

        Args:
            evt: Native DataView selection event.
            user_handler: Original event handler supplied by the caller.
        """
        # Accessibility: Ensure screen reader focus remains stable 
        # while processing selection logic.
        if self.use_dataview:
            item = evt.GetItem()
            if item.IsOk():
                # We can inject a 'GetIndex' method into the event object
                # to mimic ListEvent if necessary, or just call the handler.
                evt.GetIndex = lambda: self.control.ItemToRow(item)
        
        user_handler(evt)

    def _handle_check(self, evt, user_handler):
        """
        Normalize checkbox/toggle events across ListCtrl and DataViewListCtrl.

        Args:
            evt: Native checkbox or DataView value-changed event.
            user_handler: Original event handler supplied by the caller.
        """
        row = -1
        checked = False

        if self.use_dataview:
            if evt.GetColumn() != self._checkbox_column:
                evt.Skip()
                return
            item = evt.GetItem()
            if item.IsOk():
                row = self.control.ItemToRow(item)
                checked = self.control.GetToggleValue(row, self._checkbox_column)
        else:
            row = evt.GetIndex()
            checked = self.control.IsItemChecked(row)

        evt.GetIndex = lambda: row
        evt.IsChecked = lambda: checked
        user_handler(evt)

    def GetSelectedRow(self):
        """
        Return the currently selected row index.

        Returns:
            int: Selected row index, or -1 when no row is selected.
        """
        if self.use_dataview:
            item = self.control.GetSelection()
            return self.control.ItemToRow(item) if item.IsOk() else -1
        else:
            return self.control.GetFirstSelected()

    def GetItemCount(self):
        """
        Return the total number of items in the list.

        Returns:
            int: Number of rows in the underlying control.
        """
        return self.control.GetItemCount()

    def SelectRow(self, index):
        """
        Selects and focuses a row by index.

        Args:
            index: Zero-based row index to select.
        """
        if index < 0 or index >= self.GetItemCount():
            return

        if self.use_dataview:
            # Try to avoid ATK noise
            if self.control.GetColumnCount() > 0:
                item = self.control.RowToItem(index)
                if item.IsOk():
                    self.control.Select(item)
                    self.control.EnsureVisible(item)
        else:
            self.control.Select(index)
            self.control.Focus(index)

    def SetChecked(self, index, checked):
        """
        Set a row checkbox/toggle value without changing selection.

        Args:
            index: Zero-based row index to update.
            checked: New checked state.
        """
        if self._checkbox_column is None or index < 0 or index >= self.GetItemCount():
            return

        if self.use_dataview:
            self.control.SetToggleValue(bool(checked), index, self._checkbox_column)
        else:
            self.control.CheckItem(index, bool(checked))

    def IsChecked(self, index):
        """
        Return the row checkbox/toggle state.

        Args:
            index: Zero-based row index to inspect.

        Returns:
            bool: True when the row is checked, otherwise False.
        """
        if self._checkbox_column is None or index < 0 or index >= self.GetItemCount():
            return False

        if self.use_dataview:
            return self.control.GetToggleValue(index, self._checkbox_column)
        return self.control.IsItemChecked(index)

    def GetControl(self):
        """
        Return the wrapped wx control.

        Returns:
            wx.Window: Underlying wx.ListCtrl or DataViewListCtrl instance.
        """
        return self.control

Cross-platform list wrapper with optional checkbox support.

wx.ListCtrl is used on Windows, while wx.dataview.DataViewListCtrl is used on Linux and macOS for better accessibility. The wrapper normalizes the small subset of list operations used by the application.

Initialize universal list control.

Args

parent
Parent wx window.
size
Initial control size.
style
wx style flags applied to the underlying list control.
checkboxes
Whether rows should support checked/unchecked state.
force_dataview
Force DataViewListCtrl even on platforms that would normally use wx.ListCtrl.

Class variables

var EVT_ITEM_CHECKED

The type of the None singleton.

Methods

def Append(self, entry)
Expand source code
def Append(self, entry):
    """
    Adds a row to the list.
    'entry' must be a list or tuple of values matching the column count.

    Args:
        entry: Sequence of row values. Checkbox columns should contain
            truthy or falsy values; text columns are converted to strings.
    """
    if self.use_dataview:
        # DataViewListCtrl.AppendItem expects exactly one argument: a sequence
        # Keep toggle columns as bool and format text columns as strings.
        formatted_entry = [
            bool(item) if col_idx == self._checkbox_column else str(item)
            for col_idx, item in enumerate(entry)
        ]
        self.control.AppendItem(formatted_entry)
    else:
        # ListCtrl: Insert the first item, then set sub-items
        index = self.control.GetItemCount()
        if self._checkbox_column == 0:
            self.control.InsertItem(index, "")
            self.control.CheckItem(index, bool(entry[0]))
            text_start_col = 1
        else:
            self.control.InsertItem(index, str(entry[0]))
            text_start_col = 1
        for col_idx in range(text_start_col, len(entry)):
            self.control.SetItem(index, col_idx, str(entry[col_idx]))

Adds a row to the list. 'entry' must be a list or tuple of values matching the column count.

Args

entry
Sequence of row values. Checkbox columns should contain truthy or falsy values; text columns are converted to strings.
def Bind(self, event_type, handler)
Expand source code
def Bind(self, event_type, handler):
    """
    Unifies binding for common list events.

    Args:
        event_type: wx event binder or UniversalListCtrl event type.
        handler: Callable that receives the normalized event.
    """
    if event_type == wx.EVT_LIST_ITEM_SELECTED:
        if self.use_dataview:
            # Map DataView selection to List selection logic
            self.control.Bind(dv.EVT_DATAVIEW_SELECTION_CHANGED, 
                              lambda evt: self._handle_selection(evt, handler))
        else:
            self.control.Bind(wx.EVT_LIST_ITEM_SELECTED, handler)
    
    elif event_type in [wx.EVT_CONTEXT_MENU, wx.EVT_CHAR_HOOK]:
        # These are standard wx.Window events, no mapping needed
        self.control.Bind(event_type, handler)

    elif event_type == self.EVT_ITEM_CHECKED:
        if self.use_dataview:
            self.control.Bind(
                dv.EVT_DATAVIEW_ITEM_VALUE_CHANGED,
                lambda evt: self._handle_check(evt, handler)
            )
        else:
            self.control.Bind(
                wx.EVT_LIST_ITEM_CHECKED,
                lambda evt: self._handle_check(evt, handler)
            )
            self.control.Bind(
                wx.EVT_LIST_ITEM_UNCHECKED,
                lambda evt: self._handle_check(evt, handler)
            )
    
    else:
        # Fallback for other events
        self.control.Bind(event_type, handler)

Unifies binding for common list events.

Args

event_type
wx event binder or UniversalListCtrl event type.
handler
Callable that receives the normalized event.
def GetControl(self)
Expand source code
def GetControl(self):
    """
    Return the wrapped wx control.

    Returns:
        wx.Window: Underlying wx.ListCtrl or DataViewListCtrl instance.
    """
    return self.control

Return the wrapped wx control.

Returns

wx.Window
Underlying wx.ListCtrl or DataViewListCtrl instance.
def GetItemCount(self)
Expand source code
def GetItemCount(self):
    """
    Return the total number of items in the list.

    Returns:
        int: Number of rows in the underlying control.
    """
    return self.control.GetItemCount()

Return the total number of items in the list.

Returns

int
Number of rows in the underlying control.
def GetSelectedRow(self)
Expand source code
def GetSelectedRow(self):
    """
    Return the currently selected row index.

    Returns:
        int: Selected row index, or -1 when no row is selected.
    """
    if self.use_dataview:
        item = self.control.GetSelection()
        return self.control.ItemToRow(item) if item.IsOk() else -1
    else:
        return self.control.GetFirstSelected()

Return the currently selected row index.

Returns

int
Selected row index, or -1 when no row is selected.
def InsertColumn(self, col, heading, width=-1, checkbox=False)
Expand source code
def InsertColumn(self, col, heading, width=wx.LIST_AUTOSIZE, checkbox=False):
    """
    Insert a column in the underlying control.

    Args:
        col: Zero-based column index.
        heading: Text shown in the column header.
        width: Column width or wx autosize constant.
        checkbox: Whether this column stores checkbox/toggle values.
    """
    if self.use_dataview:
        if checkbox:
            self.control.AppendToggleColumn(
                heading,
                mode=dv.DATAVIEW_CELL_ACTIVATABLE,
                width=width
            )
            self._checkbox_column = col
        else:
            self.control.AppendTextColumn(heading, width=width)
    else:
        self.control.InsertColumn(col, heading, width=width)
        if checkbox:
            self._checkbox_column = col

Insert a column in the underlying control.

Args

col
Zero-based column index.
heading
Text shown in the column header.
width
Column width or wx autosize constant.
checkbox
Whether this column stores checkbox/toggle values.
def IsChecked(self, index)
Expand source code
def IsChecked(self, index):
    """
    Return the row checkbox/toggle state.

    Args:
        index: Zero-based row index to inspect.

    Returns:
        bool: True when the row is checked, otherwise False.
    """
    if self._checkbox_column is None or index < 0 or index >= self.GetItemCount():
        return False

    if self.use_dataview:
        return self.control.GetToggleValue(index, self._checkbox_column)
    return self.control.IsItemChecked(index)

Return the row checkbox/toggle state.

Args

index
Zero-based row index to inspect.

Returns

bool
True when the row is checked, otherwise False.
def SelectRow(self, index)
Expand source code
def SelectRow(self, index):
    """
    Selects and focuses a row by index.

    Args:
        index: Zero-based row index to select.
    """
    if index < 0 or index >= self.GetItemCount():
        return

    if self.use_dataview:
        # Try to avoid ATK noise
        if self.control.GetColumnCount() > 0:
            item = self.control.RowToItem(index)
            if item.IsOk():
                self.control.Select(item)
                self.control.EnsureVisible(item)
    else:
        self.control.Select(index)
        self.control.Focus(index)

Selects and focuses a row by index.

Args

index
Zero-based row index to select.
def SetChecked(self, index, checked)
Expand source code
def SetChecked(self, index, checked):
    """
    Set a row checkbox/toggle value without changing selection.

    Args:
        index: Zero-based row index to update.
        checked: New checked state.
    """
    if self._checkbox_column is None or index < 0 or index >= self.GetItemCount():
        return

    if self.use_dataview:
        self.control.SetToggleValue(bool(checked), index, self._checkbox_column)
    else:
        self.control.CheckItem(index, bool(checked))

Set a row checkbox/toggle value without changing selection.

Args

index
Zero-based row index to update.
checked
New checked state.