Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions source/gui/guiHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,52 @@ def associateElements( firstElement, secondElement):

return sizer


class BaseLabeledControl:
"""Base class used to create a labeled control subclass
A labeled control subclass allows to create a control with an associated label.
The associated label is enabled and disabled when the main control is enabled or disabled (respectively).

Example usage:

class LabeledChoice(BaseLabeledControl, wx.Choice):
pass
myChoice = LabeledChoice(
parent,
labelText='Choose an option',
wxCtrlClass=wx.Choice,
choices=['Option 1', 'Option 2', 'Option 3'],
)
"""

def __init__(self, parent, labelText, wxCtrlClass, **kwargs):
""" @param parent: An instance of the parent wx window. EG wx.Dialog
@param labelText: The text to associate with a wx control.
@type labelText: string
@param wxCtrlClass: The class to associate with the label, eg: wx.TextCtrl
@type wxCtrlClass: class
@param kwargs: The keyword arguments used to instantiate the wxCtrlClass
"""
self.label = wx.StaticText(parent, label=labelText)
if self.__class__ == BaseLabeledControl:
raise TypeError(
f"BaseLabeledControl should be subclassed and should also inherit from {wxCtrlClass.__name__}."
)
# Check that the final class also inherits from wxCtrlClass
if not isinstance(self, wxCtrlClass):
raise TypeError(f"{self.__class__.__name__} should be a subclass of {wxCtrlClass.__name__}.")
self.controlClass = wxCtrlClass
self.controlClass.__init__(self, parent, **kwargs)
Comment thread
feerrenrut marked this conversation as resolved.
Resolved

def Enable(self, state):
self.controlClass.Enable(self, state)
self.label.Enable(state)

def Disable(self):
self.controlClass.Disable(self)
self.label.Disable()


class LabeledControlHelper(object):
""" Represents a Labeled Control. Provides a class to create and hold on to the objects and automatically associate
the two controls together.
Expand All @@ -168,9 +214,16 @@ def __init__(self, parent, labelText, wxCtrlClass, **kwargs):
@param kwargs: The keyword arguments used to instantiate the wxCtrlClass
"""
object.__init__(self)
self._label = wx.StaticText(parent, label=labelText)
self._ctrl = wxCtrlClass(parent, **kwargs)
self._sizer = associateElements(self._label, self._ctrl)
# Dynamically create a class inheriting from BaseLabeledControl and wxCtrlClass.
# This class acts as wxCtrlClass but overloads the Enable and Disable methods
# in order to also enable or disable self._label at the same time as the control.
LabeledControl = type(
f'Labeled{wxCtrlClass.__name__}',
(BaseLabeledControl, wxCtrlClass),
{},
)
self._ctrl = LabeledControl(parent, labelText=labelText, wxCtrlClass=wxCtrlClass, **kwargs)
self._sizer = associateElements(self._ctrl.label, self._ctrl)

Comment thread
CyrilleB79 marked this conversation as resolved.
Resolved
@property
def control(self):
Expand Down