@@ -17,6 +17,7 @@ def __init__(
1717 toggle_color : str = "#c1e2c5" ,
1818 disable_color : str = "lightgrey" ,
1919 toggle_group : Optional [Any ] = None ,
20+ skip_runtime_errors : bool = True ,
2021 ** kwargs ,
2122 ):
2223 """
@@ -27,28 +28,31 @@ def __init__(
2728 toggle_color (str): Color when toggled.
2829 disable_color (str): Color when disabled.
2930 toggle_group (Optional[CTkToggleGroup]): Group to coordinate toggling.
31+ skip_runtime_errors (bool): If True, suppress RuntimeErrors raised by state
32+ guard checks in toggle(), get_toggle_state(), enable(), and disable().
3033 **kwargs: Additional keyword arguments for CTkButton.
31-
34+
3235 Raises:
3336 ValueError: If toggle_color or disable_color is not a string.
3437 TypeError: If toggle_group is not an instance of CTkToggleGroup or None.
3538 """
3639 from .ctk_toggle_group import CTkToggleGroup
37-
40+
3841 if not isinstance (toggle_color , str ):
3942 raise ValueError ("toggle_color must be a string." )
4043 if not isinstance (disable_color , str ):
4144 raise ValueError ("disable_color must be a string." )
4245 if toggle_group is not None and not isinstance (toggle_group , CTkToggleGroup ):
4346 raise TypeError ("toggle_group must be an instance of CTkToggleGroup or None." )
44-
47+
4548 super ().__init__ (master , ** kwargs )
4649 self .toggle_state = False
4750 self .toggle_color = toggle_color
4851 self .disable_color = disable_color
4952 self .default_fg_color = self .cget ("fg_color" )
5053 self .toggle_group = toggle_group
5154 self ._state = "normal"
55+ self .skip_runtime_errors = skip_runtime_errors
5256 self .bind ("<Button-1>" , self .toggle )
5357
5458 def toggle (self , event = None ):
@@ -68,7 +72,8 @@ def toggle(self, event=None):
6872 self .toggle_state = not self .toggle_state
6973 self ._update_fg_color ()
7074 else :
71- raise RuntimeError ("Cannot toggle button when it is disabled." )
75+ if not self .skip_runtime_errors :
76+ raise RuntimeError ("Cannot toggle button when it is disabled." )
7277
7378 def set_toggle_state (self , state : bool ):
7479 """
@@ -97,7 +102,8 @@ def get_toggle_state(self) -> bool:
97102 RuntimeError: If the button is in an invalid state.
98103 """
99104 if self ._state != "normal" :
100- raise RuntimeError ("Cannot get toggle state when button is disabled." )
105+ if not self .skip_runtime_errors :
106+ raise RuntimeError ("Cannot get toggle state when button is disabled." )
101107
102108 return self .toggle_state
103109
@@ -109,7 +115,9 @@ def enable(self):
109115 RuntimeError: If the button is already enabled.
110116 """
111117 if self ._state == "normal" :
112- raise RuntimeError ("Button is already enabled." )
118+ if not self .skip_runtime_errors :
119+ raise RuntimeError ("Button is already enabled." )
120+ return
113121
114122 self ._state = "normal"
115123 if not self .toggle_state :
@@ -123,7 +131,9 @@ def disable(self):
123131 RuntimeError: If the button is already disabled.
124132 """
125133 if self ._state == "disabled" :
126- raise RuntimeError ("Button is already disabled." )
134+ if not self .skip_runtime_errors :
135+ raise RuntimeError ("Button is already disabled." )
136+ return
127137
128138 self ._state = "disabled"
129139 self .set_toggle_state (False )
@@ -136,5 +146,4 @@ def _update_fg_color(self):
136146 Update the foreground color based on the toggle state.
137147 """
138148 new_color = self .toggle_color if self .toggle_state else self .default_fg_color
139- self .configure (fg_color = new_color )
140-
149+ self .configure (fg_color = new_color )
0 commit comments