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