@@ -1176,9 +1176,9 @@ def set_check_props(self, props):
1176
1176
# If new colours are supplied, then we must re-apply the status.
1177
1177
self ._init_status (actives )
1178
1178
1179
- def set_active (self , index ):
1179
+ def set_active (self , index , state = None ):
1180
1180
"""
1181
- Toggle (activate or deactivate) a check button by index.
1181
+ Modify the state of a check button by index.
1182
1182
1183
1183
Callbacks will be triggered if :attr:`eventson` is True.
1184
1184
@@ -1187,22 +1187,27 @@ def set_active(self, index):
1187
1187
index : int
1188
1188
Index of the check button to toggle.
1189
1189
1190
+ state : bool, optional
1191
+ If a boolean value, set the state explicitly. If no value is
1192
+ provided, the state is toggled.
1193
+
1190
1194
Raises
1191
1195
------
1192
1196
ValueError
1193
1197
If *index* is invalid.
1198
+ TypeError
1199
+ If *state* is not boolean.
1194
1200
"""
1195
1201
if index not in range (len (self .labels )):
1196
1202
raise ValueError (f'Invalid CheckButton index: { index } ' )
1203
+ _api .check_isinstance ((bool , None ), state = state )
1197
1204
1198
1205
invisible = colors .to_rgba ('none' )
1199
1206
1200
1207
facecolors = self ._checks .get_facecolor ()
1201
- facecolors [index ] = (
1202
- self ._active_check_colors [index ]
1203
- if colors .same_color (facecolors [index ], invisible )
1204
- else invisible
1205
- )
1208
+ if state is None :
1209
+ state = colors .same_color (facecolors [index ], invisible )
1210
+ facecolors [index ] = self ._active_check_colors [index ] if state else invisible
1206
1211
self ._checks .set_facecolor (facecolors )
1207
1212
1208
1213
if self .drawon :
@@ -1233,18 +1238,55 @@ def _init_status(self, actives):
1233
1238
[ec if active else "none"
1234
1239
for ec , active in zip (self ._active_check_colors , actives )])
1235
1240
1241
+ def clear (self ):
1242
+ """Uncheck all checkboxes."""
1243
+
1244
+ self ._checks .set_facecolor (['none' ] * len (self ._active_check_colors ))
1245
+
1246
+ if hasattr (self , '_lines' ):
1247
+ for l1 , l2 in self ._lines :
1248
+ l1 .set_visible (False )
1249
+ l2 .set_visible (False )
1250
+
1251
+ if self .drawon :
1252
+ self .canvas .draw ()
1253
+
1254
+ if self .eventson :
1255
+ # Call with no label, as all checkboxes are being cleared.
1256
+ self ._observers .process ('clicked' , None )
1257
+
1236
1258
def get_status (self ):
1237
1259
"""
1238
1260
Return a list of the status (True/False) of all of the check buttons.
1239
1261
"""
1240
1262
return [not colors .same_color (color , colors .to_rgba ("none" ))
1241
1263
for color in self ._checks .get_facecolors ()]
1242
1264
1265
+ def get_checked_labels (self ):
1266
+ """Return a list of labels currently checked by user."""
1267
+
1268
+ return [l .get_text () for l , box_checked in
1269
+ zip (self .labels , self .get_status ())
1270
+ if box_checked ]
1271
+
1243
1272
def on_clicked (self , func ):
1244
1273
"""
1245
1274
Connect the callback function *func* to button click events.
1246
1275
1247
- Returns a connection id, which can be used to disconnect the callback.
1276
+ Parameters
1277
+ ----------
1278
+ func : callable
1279
+ When the button is clicked, call *func* with button label.
1280
+ When all buttons are cleared, call *func* with None.
1281
+ The callback func must have the signature::
1282
+
1283
+ def func(label: str | None) -> Any
1284
+
1285
+ Return values may exist, but are ignored.
1286
+
1287
+ Returns
1288
+ -------
1289
+ A connection id, which can be used to disconnect the callback.
1248
1290
"""
1249
1291
return self ._observers .connect ('clicked' , lambda text : func (text ))
1250
1292
@@ -1533,6 +1575,8 @@ class RadioButtons(AxesWidget):
1533
1575
The buttons.
1534
1576
value_selected : str
1535
1577
The label text of the currently selected button.
1578
+ index_selected : int
1579
+ The index of the selected button.
1536
1580
"""
1537
1581
1538
1582
def __init__ (self , ax , labels , active = 0 , activecolor = None , * ,
@@ -1590,7 +1634,9 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
1590
1634
activecolor = 'blue' # Default.
1591
1635
1592
1636
self ._activecolor = activecolor
1637
+ self ._initial_active = active
1593
1638
self .value_selected = labels [active ]
1639
+ self .index_selected = active
1594
1640
1595
1641
ax .set_xticks ([])
1596
1642
ax .set_yticks ([])
@@ -1716,10 +1762,21 @@ def set_active(self, index):
1716
1762
Select button with number *index*.
1717
1763
1718
1764
Callbacks will be triggered if :attr:`eventson` is True.
1765
+
1766
+ Parameters
1767
+ ----------
1768
+ index : int
1769
+ The index of the button to activate.
1770
+
1771
+ Raises
1772
+ ------
1773
+ ValueError
1774
+ If the index is invalid.
1719
1775
"""
1720
1776
if index not in range (len (self .labels )):
1721
1777
raise ValueError (f'Invalid RadioButton index: { index } ' )
1722
1778
self .value_selected = self .labels [index ].get_text ()
1779
+ self .index_selected = index
1723
1780
button_facecolors = self ._buttons .get_facecolor ()
1724
1781
button_facecolors [:] = colors .to_rgba ("none" )
1725
1782
button_facecolors [index ] = colors .to_rgba (self ._active_colors [index ])
@@ -1737,11 +1794,28 @@ def set_active(self, index):
1737
1794
if self .eventson :
1738
1795
self ._observers .process ('clicked' , self .labels [index ].get_text ())
1739
1796
1797
+ def clear (self ):
1798
+ """Reset the active button to the initially active one."""
1799
+ self .set_active (self ._initial_active )
1800
+
1740
1801
def on_clicked (self , func ):
1741
1802
"""
1742
1803
Connect the callback function *func* to button click events.
1743
1804
1744
- Returns a connection id, which can be used to disconnect the callback.
1805
+ Parameters
1806
+ ----------
1807
+ func : callable
1808
+ When the button is clicked, call *func* with button label.
1809
+ When all buttons are cleared, call *func* with None.
1810
+ The callback func must have the signature::
1811
+
1812
+ def func(label: str | None) -> Any
1813
+
1814
+ Return values may exist, but are ignored.
1815
+
1816
+ Returns
1817
+ -------
1818
+ A connection id, which can be used to disconnect the callback.
1745
1819
"""
1746
1820
return self ._observers .connect ('clicked' , func )
1747
1821
0 commit comments