This commit is contained in:
demberto
2023-04-10 17:41:14 +05:30
parent 09e584634c
commit a9b44c180e
48 changed files with 968 additions and 869 deletions

View File

@ -1,11 +1,12 @@
import tkinter
import sys
from typing import Union, Tuple, Callable, Optional
from __future__ import annotations
from .core_rendering import CTkCanvas
from .theme import ThemeManager
from .core_rendering import DrawEngine
import sys
import tkinter
from typing import Any, Callable
from .core_rendering import CTkCanvas, DrawEngine
from .core_widget_classes import CTkBaseClass
from .theme import ThemeManager
class CTkSlider(CTkBaseClass):
@ -15,30 +16,30 @@ class CTkSlider(CTkBaseClass):
"""
def __init__(self,
master: any,
width: Optional[int] = None,
height: Optional[int] = None,
corner_radius: Optional[int] = None,
button_corner_radius: Optional[int] = None,
border_width: Optional[int] = None,
button_length: Optional[int] = None,
master: CTkBaseClass,
width: int | None = None,
height: int | None = None,
corner_radius: int | None = None,
button_corner_radius: int | None = None,
border_width: int | None = None,
button_length: int | None = None,
bg_color: Union[str, Tuple[str, str]] = "transparent",
fg_color: Optional[Union[str, Tuple[str, str]]] = None,
border_color: Union[str, Tuple[str, str]] = "transparent",
progress_color: Optional[Union[str, Tuple[str, str]]] = None,
button_color: Optional[Union[str, Tuple[str, str]]] = None,
button_hover_color: Optional[Union[str, Tuple[str, str]]] = None,
bg_color: str | tuple[str, str] = "transparent",
fg_color: str | tuple[str, str] | None = None,
border_color: str | tuple[str, str] = "transparent",
progress_color: str | tuple[str, str] | None = None,
button_color: str | tuple[str, str] | None = None,
button_hover_color: str | tuple[str, str] | None = None,
from_: int = 0,
to: int = 1,
state: str = "normal",
number_of_steps: Union[int, None] = None,
number_of_steps: int | None = None,
hover: bool = True,
command: Union[Callable[[float], None], None] = None,
variable: Union[tkinter.Variable, None] = None,
command: Callable[[float], None] | None = None,
variable: tkinter.Variable | None = None,
orientation: str = "horizontal",
**kwargs):
**kwargs: Any):
# set default dimensions according to orientation
if width is None:
@ -81,9 +82,9 @@ class CTkSlider(CTkBaseClass):
# callback and control variables
self._command = command
self._variable: tkinter.Variable = variable
self._variable_callback_blocked: bool = False
self._variable_callback_name: Union[bool, None] = None
self._variable = variable
self._variable_callback_blocked = False
self._variable_callback_name: bool | None = None
self._state = state
self.grid_rowconfigure(0, weight=1)
@ -106,7 +107,7 @@ class CTkSlider(CTkBaseClass):
self.set(self._variable.get(), from_variable_callback=True)
self._variable_callback_blocked = False
def _create_bindings(self, sequence: Optional[str] = None):
def _create_bindings(self, sequence: str | None = None):
""" set necessary bindings for functionality of widget, will overwrite other bindings """
if sequence is None or sequence == "<Enter>":
self._canvas.bind("<Enter>", self._on_enter)
@ -117,14 +118,14 @@ class CTkSlider(CTkBaseClass):
if sequence is None or sequence == "<B1-Motion>":
self._canvas.bind("<B1-Motion>", self._clicked)
def _set_scaling(self, *args, **kwargs):
def _set_scaling(self, *args: Any, **kwargs: Any):
super()._set_scaling(*args, **kwargs)
self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
height=self._apply_widget_scaling(self._desired_height))
self._draw(no_color_updates=True)
def _set_dimensions(self, width=None, height=None):
def _set_dimensions(self, width: int | None = None, height: int | None = None):
super()._set_dimensions(width, height)
self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
@ -151,7 +152,7 @@ class CTkSlider(CTkBaseClass):
elif sys.platform.startswith("win"):
self.configure(cursor="arrow")
def _draw(self, no_color_updates=False):
def _draw(self, no_color_updates: bool = False):
super()._draw(no_color_updates)
if self._orientation.lower() == "horizontal":
@ -198,7 +199,7 @@ class CTkSlider(CTkBaseClass):
fill=self._apply_appearance_mode(self._button_color),
outline=self._apply_appearance_mode(self._button_color))
def configure(self, require_redraw=False, **kwargs):
def configure(self, require_redraw: bool = False, **kwargs: Any):
if "state" in kwargs:
self._state = kwargs.pop("state")
self._set_cursor()
@ -257,7 +258,7 @@ class CTkSlider(CTkBaseClass):
super().configure(require_redraw=require_redraw, **kwargs)
def cget(self, attribute_name: str) -> any:
def cget(self, attribute_name: str) -> Any:
if attribute_name == "corner_radius":
return self._corner_radius
elif attribute_name == "button_corner_radius":
@ -298,7 +299,7 @@ class CTkSlider(CTkBaseClass):
else:
return super().cget(attribute_name)
def _clicked(self, event=None):
def _clicked(self, event: tkinter.Event[Any] | None = None):
if self._state == "normal":
if self._orientation.lower() == "horizontal":
self._value = self._reverse_widget_scaling(event.x / self._current_width)
@ -323,14 +324,14 @@ class CTkSlider(CTkBaseClass):
if self._command is not None:
self._command(self._output_value)
def _on_enter(self, event=0):
def _on_enter(self, event: tkinter.Event[Any] | None = None):
if self._hover is True and self._state == "normal":
self._hover_state = True
self._canvas.itemconfig("slider_parts",
fill=self._apply_appearance_mode(self._button_hover_color),
outline=self._apply_appearance_mode(self._button_hover_color))
def _on_leave(self, event=0):
def _on_leave(self, event: tkinter.Event[Any] | None = None):
self._hover_state = False
self._canvas.itemconfig("slider_parts",
fill=self._apply_appearance_mode(self._button_color),
@ -347,7 +348,7 @@ class CTkSlider(CTkBaseClass):
def get(self) -> float:
return self._output_value
def set(self, output_value, from_variable_callback=False):
def set(self, output_value, from_variable_callback: bool = False):
if self._from_ < self._to:
if output_value > self._to:
output_value = self._to
@ -369,17 +370,17 @@ class CTkSlider(CTkBaseClass):
self._variable.set(round(self._output_value) if isinstance(self._variable, tkinter.IntVar) else self._output_value)
self._variable_callback_blocked = False
def _variable_callback(self, var_name, index, mode):
def _variable_callback(self, var_name: str, index: int, mode: Any):
if not self._variable_callback_blocked:
self.set(self._variable.get(), from_variable_callback=True)
def bind(self, sequence: str = None, command: Callable = None, add: Union[str, bool] = True):
def bind(self, sequence: str, command: Callable[..., None] | None = None, add: str | bool = True):
""" called on the tkinter.Canvas """
if not (add == "+" or add is True):
raise ValueError("'add' argument can only be '+' or True to preserve internal callbacks")
self._canvas.bind(sequence, command, add=True)
def unbind(self, sequence: str = None, funcid: str = None):
def unbind(self, sequence: str, funcid: str | None = None):
""" called on the tkinter.Label and tkinter.Canvas """
if funcid is not None:
raise ValueError("'funcid' argument can only be None, because there is a bug in" +