diff --git a/src/batgrl/gadgets/button.py b/src/batgrl/gadgets/button.py index d35940ad..5cc80435 100644 --- a/src/batgrl/gadgets/button.py +++ b/src/batgrl/gadgets/button.py @@ -35,12 +35,12 @@ class Button(Themable, ButtonBehavior, Gadget): ---------- label : str, default: "" Button label. - callback : Callable[[], None], default: lambda: None + callback : Callable[[], None] | None, default: None Called when button is released. - always_release : bool, default: False - Whether a mouse up event outside the button will trigger it. alpha : float, default: 1.0 Transparency of gadget. + always_release : bool, default: False + Whether a mouse up event outside the button will trigger it. size : Size, default: Size(10, 10) Size of gadget. pos : Point, default: Point(0, 0) @@ -62,14 +62,14 @@ class Button(Themable, ButtonBehavior, Gadget): ---------- label : str Button label. - callback : Callable[[], None] + callback : Callable[[], None] | None Called when button is released. + alpha : float + Transparency of gadget. always_release : bool Whether a mouse up event outside the button will trigger it. state : ButtonState Current button state. One of `NORMAL`, `HOVER`, `DOWN`. - alpha : float - Transparency of gadget. size : Size Size of gadget. height : int @@ -181,11 +181,11 @@ def __init__( self, *, label: str = "", - callback: Callable[[], None] = lambda: None, + callback: Callable[[], None] | None = None, always_release: bool = False, + alpha: float = 1.0, size=Size(10, 10), pos=Point(0, 0), - alpha: float = 1.0, size_hint: SizeHint | SizeHintDict | None = None, pos_hint: PosHint | PosHintDict | None = None, is_transparent: bool = False, @@ -264,4 +264,5 @@ def update_down(self): def on_release(self): """Triggered when button is released.""" - self.callback() + if self.callback is not None: + self.callback() diff --git a/src/batgrl/gadgets/toggle_button.py b/src/batgrl/gadgets/toggle_button.py index 05fdafe6..68000203 100644 --- a/src/batgrl/gadgets/toggle_button.py +++ b/src/batgrl/gadgets/toggle_button.py @@ -48,8 +48,10 @@ class ToggleButton(Themable, ToggleButtonBehavior, Gadget): ---------- label : str, default: "" Toggle button label. - callback : Callable[[ToggleState], None], default: lambda: None + callback : Callable[[ToggleState], None] | None, default: None Called when toggle state changes. The new state is provided as first argument. + alpha : float, default: 1.0 + Transparency of gadget. group : None | Hashable, default: None If a group is provided, only one button in a group can be in the "on" state. allow_no_selection : bool, default: False @@ -59,8 +61,6 @@ class ToggleButton(Themable, ToggleButtonBehavior, Gadget): Initial toggle state of button. always_release : bool, default: False Whether a mouse up event outside the button will trigger it. - alpha : float, default: 1.0 - Transparency of gadget. size : Size, default: Size(10, 10) Size of gadget. pos : Point, default: Point(0, 0) @@ -82,8 +82,10 @@ class ToggleButton(Themable, ToggleButtonBehavior, Gadget): ---------- label : str Toggle button label. - callback : Callable[[ToggleState], None] - Button callback when toggled. + callback : Callable[[ToggleState], None] | None + Called when toggle state changes. + alpha : float + Transparency of gadget. group : None | Hashable If a group is provided, only one button in a group can be in the "on" state. allow_no_selection : bool @@ -94,8 +96,6 @@ class ToggleButton(Themable, ToggleButtonBehavior, Gadget): Whether a mouse up event outside the button will trigger it. state : ButtonState Current button state. One of `NORMAL`, `HOVER`, `DOWN`. - alpha : float - Transparency of gadget. size : Size Size of gadget. height : int @@ -213,12 +213,12 @@ def __init__( self, *, label: str = "", - callback: Callable[[ToggleState], None] = lambda state: None, + callback: Callable[[ToggleState], None] | None = None, + alpha: float = 1.0, group: None | Hashable = None, allow_no_selection: bool = False, toggle_state: ToggleState = ToggleState.OFF, always_release: bool = False, - alpha: float = 1.0, size=Size(10, 10), pos=Point(0, 0), size_hint: SizeHint | SizeHintDict | None = None, @@ -312,6 +312,9 @@ def update_down(self): def on_toggle(self): """Call callback on toggle state change.""" - if self.root: - self.label = self.label # Update radio button/checkbox + if self.root is None: + return + + self.label = self.label # Update radio button/checkbox + if self.callback is not None: self.callback(self.toggle_state)