@@ -1198,7 +1198,7 @@ def set_check_props(self, props):
1198
1198
# If new colours are supplied, then we must re-apply the status.
1199
1199
self ._init_status (actives )
1200
1200
1201
- def set_active (self , index ):
1201
+ def set_active (self , index , state = None ):
1202
1202
"""
1203
1203
Toggle (activate or deactivate) a check button by index.
1204
1204
@@ -1209,22 +1209,27 @@ def set_active(self, index):
1209
1209
index : int
1210
1210
Index of the check button to toggle.
1211
1211
1212
+ state : bool, optional
1213
+ Sets the target state of the button. If a boolean value, set the
1214
+ state explicitly. If no value is provided, the state is toggled.
1215
+
1212
1216
Raises
1213
1217
------
1214
1218
ValueError
1215
1219
If *index* is invalid.
1220
+ TypeError
1221
+ If *state* is not boolean.
1216
1222
"""
1217
1223
if index not in range (len (self .labels )):
1218
1224
raise ValueError (f'Invalid CheckButton index: { index } ' )
1225
+ _api .check_isinstance ((bool , None ), state = state )
1219
1226
1220
1227
invisible = colors .to_rgba ('none' )
1221
1228
1222
1229
facecolors = self ._checks .get_facecolor ()
1223
- facecolors [index ] = (
1224
- self ._active_check_colors [index ]
1225
- if colors .same_color (facecolors [index ], invisible )
1226
- else invisible
1227
- )
1230
+ if state is None :
1231
+ state = colors .same_color (facecolors [index ], invisible )
1232
+ facecolors [index ] = self ._active_check_colors [index ] if state else invisible
1228
1233
self ._checks .set_facecolor (facecolors )
1229
1234
1230
1235
if hasattr (self , "_lines" ):
@@ -1264,18 +1269,55 @@ def _init_status(self, actives):
1264
1269
[ec if active else "none"
1265
1270
for ec , active in zip (self ._active_check_colors , actives )])
1266
1271
1272
+ def clear (self ):
1273
+ """Clear all checkboxes."""
1274
+
1275
+ self ._checks .set_facecolor (['none' ] * len (self ._active_check_colors ))
1276
+
1277
+ if hasattr (self , '_lines' ):
1278
+ for l1 , l2 in self ._lines :
1279
+ l1 .set_visible (False )
1280
+ l2 .set_visible (False )
1281
+
1282
+ if self .drawon :
1283
+ self .canvas .draw ()
1284
+
1285
+ if self .eventson :
1286
+ # Call with no label, as all checkboxes are being cleared.
1287
+ self ._observers .process ('clicked' , None )
1288
+
1267
1289
def get_status (self ):
1268
1290
"""
1269
1291
Return a list of the status (True/False) of all of the check buttons.
1270
1292
"""
1271
1293
return [not colors .same_color (color , colors .to_rgba ("none" ))
1272
1294
for color in self ._checks .get_facecolors ()]
1273
1295
1296
+ def get_checked_labels (self ):
1297
+ """Return a list of labels currently checked by user."""
1298
+
1299
+ return [l .get_text () for l , box_checked in
1300
+ zip (self .labels , self .get_status ())
1301
+ if box_checked ]
1302
+
1274
1303
def on_clicked (self , func ):
1275
1304
"""
1276
1305
Connect the callback function *func* to button click events.
1277
1306
1278
- Returns a connection id, which can be used to disconnect the callback.
1307
+ Parameters
1308
+ ----------
1309
+ func : callable
1310
+ When the button is clicked, call *func* with button label.
1311
+ When all buttons are cleared, call *func* with None.
1312
+ The callback func must have the signature::
1313
+
1314
+ def func(label: str | None) -> Any
1315
+
1316
+ Return values may exist, but are ignored.
1317
+
1318
+ Returns
1319
+ -------
1320
+ A connection id, which can be used to disconnect the callback.
1279
1321
"""
1280
1322
return self ._observers .connect ('clicked' , lambda text : func (text ))
1281
1323
@@ -1620,6 +1662,8 @@ class RadioButtons(AxesWidget):
1620
1662
The buttons.
1621
1663
value_selected : str
1622
1664
The label text of the currently selected button.
1665
+ index_selected : int
1666
+ The index of the selected button.
1623
1667
"""
1624
1668
1625
1669
def __init__ (self , ax , labels , active = 0 , activecolor = None , * ,
@@ -1677,7 +1721,9 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,
1677
1721
activecolor = 'blue' # Default.
1678
1722
1679
1723
self ._activecolor = activecolor
1724
+ self ._initial_active = active
1680
1725
self .value_selected = labels [active ]
1726
+ self .index_selected = active
1681
1727
1682
1728
ax .set_xticks ([])
1683
1729
ax .set_yticks ([])
@@ -1816,10 +1862,21 @@ def set_active(self, index):
1816
1862
Select button with number *index*.
1817
1863
1818
1864
Callbacks will be triggered if :attr:`eventson` is True.
1865
+
1866
+ Parameters
1867
+ ----------
1868
+ index : int
1869
+ The index of the button to activate.
1870
+
1871
+ Raises
1872
+ ------
1873
+ ValueError
1874
+ If the index is invalid.
1819
1875
"""
1820
1876
if index not in range (len (self .labels )):
1821
1877
raise ValueError (f'Invalid RadioButton index: { index } ' )
1822
1878
self .value_selected = self .labels [index ].get_text ()
1879
+ self .index_selected = index
1823
1880
button_facecolors = self ._buttons .get_facecolor ()
1824
1881
button_facecolors [:] = colors .to_rgba ("none" )
1825
1882
button_facecolors [index ] = colors .to_rgba (self ._active_colors [index ])
@@ -1844,11 +1901,28 @@ def set_active(self, index):
1844
1901
if self .eventson :
1845
1902
self ._observers .process ('clicked' , self .labels [index ].get_text ())
1846
1903
1904
+ def clear (self ):
1905
+ """Reset active button."""
1906
+ self .set_active (self ._initial_active )
1907
+
1847
1908
def on_clicked (self , func ):
1848
1909
"""
1849
1910
Connect the callback function *func* to button click events.
1850
1911
1851
- Returns a connection id, which can be used to disconnect the callback.
1912
+ Parameters
1913
+ ----------
1914
+ func : callable
1915
+ When the button is clicked, call *func* with button label.
1916
+ When all buttons are cleared, call *func* with None.
1917
+ The callback func must have the signature::
1918
+
1919
+ def func(label: str | None) -> Any
1920
+
1921
+ Return values may exist, but are ignored.
1922
+
1923
+ Returns
1924
+ -------
1925
+ A connection id, which can be used to disconnect the callback.
1852
1926
"""
1853
1927
return self ._observers .connect ('clicked' , func )
1854
1928
0 commit comments