Skip to content

Commit

Permalink
add the 'initial' and 'toggled' kwargs to ToggleSwitch
Browse files Browse the repository at this point in the history
  • Loading branch information
jborbely committed Aug 22, 2022
1 parent 5f506fc commit c9e1f59
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
11 changes: 6 additions & 5 deletions msl/examples/qt/toggle_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ def show():
app = application()
window = QtWidgets.QWidget()
window.setWindowTitle('Toggle Switch Example')
hbox = QtWidgets.QHBoxLayout()
ts = ToggleSwitch(parent=window)
ts.toggled.connect(print_state)
hbox.addWidget(ts)
window.setLayout(hbox)
layout = QtWidgets.QVBoxLayout()
ts1 = ToggleSwitch(toggled=print_state)
ts2 = ToggleSwitch(initial=True, on_color='red', tooltip='Not connected')
layout.addWidget(ts1)
layout.addWidget(ts2)
window.setLayout(layout)
window.show()
app.exec()

Expand Down
18 changes: 14 additions & 4 deletions msl/qt/widgets/toggle_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,31 @@

class ToggleSwitch(QtWidgets.QAbstractButton):

def __init__(self, *, parent=None, height=None, on_color='#009688', off_color='#B4B4B4', tooltip=None):
"""Constructs a toggle switch, |toggle_switch|
def __init__(self, *, height=None, initial=False, on_color='#009688', off_color='#B4B4B4',
parent=None, toggled=None, tooltip=None):
"""A toggle switch, |toggle_switch|, :class:`QtWidgets.QWidget`
.. |toggle_switch| image:: ../../docs/_static/toggle_switch.gif
:scale: 40 %
Parameters
----------
parent : :class:`QtWidgets.QWidget`, optional
The parent widget.
height : :class:`int`, optional
The height, in pixels, of the toggle switch.
initial : :class:`bool`, optional
Whether the toggle switch is initially in the on (:data:`True`) or off
(:data:`False`) state.
on_color
The color when the :class:`ToggleSwitch` is on. See :func:`~.convert.to_qcolor`
for details about the different data types that are supported.
off_color
The color when the :class:`ToggleSwitch` is off. See :func:`~.convert.to_qcolor`
for details about the different data types that are supported.
parent : :class:`QtWidgets.QWidget`, optional
The parent widget.
toggled
A callable function to use as a slot for the
:meth:`~QtWidgets.QAbstractButton.toggled` signal.
tooltip : :class:`str`, optional
The tooltip to use for the :class:`ToggleSwitch`.
Expand All @@ -50,10 +57,13 @@ def __init__(self, *, parent=None, height=None, on_color='#009688', off_color='#

self._pad = 4
self.setCheckable(True)
self.setChecked(initial)
self.set_on_color(on_color)
self.set_off_color(off_color)
if tooltip:
self.setToolTip(tooltip)
if toggled:
self.toggled.connect(toggled)

@property
def is_on(self):
Expand Down

0 comments on commit c9e1f59

Please sign in to comment.