2021-03-04 20:27:46 +03:00
|
|
|
import tkinter
|
|
|
|
import sys
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
from .ctk_canvas import CTkCanvas
|
2022-05-17 22:44:59 +03:00
|
|
|
from ..theme_manager import ThemeManager
|
|
|
|
from ..settings import Settings
|
|
|
|
from ..draw_engine import DrawEngine
|
2022-04-20 23:50:57 +03:00
|
|
|
from .widget_base_class import CTkBaseClass
|
2021-03-04 20:27:46 +03:00
|
|
|
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
class CTkSlider(CTkBaseClass):
|
2022-05-31 23:32:21 +03:00
|
|
|
""" tkinter custom slider"""
|
2021-09-13 15:27:29 +03:00
|
|
|
|
2022-01-16 18:54:21 +03:00
|
|
|
def __init__(self, *args,
|
2021-03-04 20:27:46 +03:00
|
|
|
bg_color=None,
|
|
|
|
border_color=None,
|
2022-02-23 00:38:40 +03:00
|
|
|
fg_color="default_theme",
|
|
|
|
progress_color="default_theme",
|
|
|
|
button_color="default_theme",
|
|
|
|
button_hover_color="default_theme",
|
2021-09-13 15:27:29 +03:00
|
|
|
from_=0,
|
|
|
|
to=1,
|
2021-12-20 13:55:07 +03:00
|
|
|
number_of_steps=None,
|
2022-05-16 17:51:19 +03:00
|
|
|
width=None,
|
|
|
|
height=None,
|
2022-02-28 02:35:52 +03:00
|
|
|
corner_radius="default_theme",
|
2022-03-01 18:12:55 +03:00
|
|
|
button_corner_radius="default_theme",
|
2022-02-28 02:35:52 +03:00
|
|
|
border_width="default_theme",
|
2022-03-01 18:12:55 +03:00
|
|
|
button_length="default_theme",
|
2021-03-04 20:27:46 +03:00
|
|
|
command=None,
|
2022-01-16 18:54:21 +03:00
|
|
|
variable=None,
|
2022-05-16 17:51:19 +03:00
|
|
|
orient="horizontal",
|
2022-06-15 03:07:51 +03:00
|
|
|
state="normal",
|
2022-01-16 18:54:21 +03:00
|
|
|
**kwargs):
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-05-16 17:51:19 +03:00
|
|
|
# set default dimensions according to orientation
|
|
|
|
if width is None:
|
|
|
|
if orient.lower() == "vertical":
|
|
|
|
width = 16
|
|
|
|
else:
|
|
|
|
width = 200
|
|
|
|
if height is None:
|
|
|
|
if orient.lower() == "vertical":
|
|
|
|
height = 200
|
|
|
|
else:
|
|
|
|
height = 16
|
|
|
|
|
2022-05-31 23:32:21 +03:00
|
|
|
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
2022-04-20 23:50:57 +03:00
|
|
|
super().__init__(*args, bg_color=bg_color, width=width, height=height, **kwargs)
|
2022-01-07 22:54:45 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# color
|
2022-01-07 22:54:45 +03:00
|
|
|
self.border_color = border_color
|
2022-05-17 22:44:59 +03:00
|
|
|
self.fg_color = ThemeManager.theme["color"]["slider"] if fg_color == "default_theme" else fg_color
|
|
|
|
self.progress_color = ThemeManager.theme["color"]["slider_progress"] if progress_color == "default_theme" else progress_color
|
|
|
|
self.button_color = ThemeManager.theme["color"]["slider_button"] if button_color == "default_theme" else button_color
|
|
|
|
self.button_hover_color = ThemeManager.theme["color"]["slider_button_hover"] if button_hover_color == "default_theme" else button_hover_color
|
2021-09-14 15:48:17 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# shape
|
2022-05-17 22:44:59 +03:00
|
|
|
self.corner_radius = ThemeManager.theme["shape"]["slider_corner_radius"] if corner_radius == "default_theme" else corner_radius
|
|
|
|
self.button_corner_radius = ThemeManager.theme["shape"]["slider_button_corner_radius"] if button_corner_radius == "default_theme" else button_corner_radius
|
|
|
|
self.border_width = ThemeManager.theme["shape"]["slider_border_width"] if border_width == "default_theme" else border_width
|
|
|
|
self.button_length = ThemeManager.theme["shape"]["slider_button_length"] if button_length == "default_theme" else button_length
|
2021-11-07 00:06:27 +03:00
|
|
|
self.value = 0.5 # initial value of slider in percent
|
2022-06-19 22:16:19 +03:00
|
|
|
self.orientation = orient
|
2021-03-04 20:27:46 +03:00
|
|
|
self.hover_state = False
|
2021-09-13 15:27:29 +03:00
|
|
|
self.from_ = from_
|
|
|
|
self.to = to
|
2021-12-20 13:55:07 +03:00
|
|
|
self.number_of_steps = number_of_steps
|
2021-09-13 15:27:29 +03:00
|
|
|
self.output_value = self.from_ + (self.value * (self.to - self.from_))
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-03-08 17:03:47 +03:00
|
|
|
if self.corner_radius < self.button_corner_radius:
|
|
|
|
self.corner_radius = self.button_corner_radius
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# callback and control variables
|
2022-07-05 15:39:12 +03:00
|
|
|
self.command = command
|
2022-01-16 18:54:21 +03:00
|
|
|
self.variable: tkinter.Variable = variable
|
|
|
|
self.variable_callback_blocked = False
|
2022-02-28 02:35:52 +03:00
|
|
|
self.variable_callback_name = None
|
2022-06-15 03:07:51 +03:00
|
|
|
self.state = state
|
2022-01-16 18:54:21 +03:00
|
|
|
|
2022-05-02 00:29:14 +03:00
|
|
|
self.grid_rowconfigure(0, weight=1)
|
|
|
|
self.grid_columnconfigure(0, weight=1)
|
|
|
|
|
2022-03-01 18:12:55 +03:00
|
|
|
self.canvas = CTkCanvas(master=self,
|
|
|
|
highlightthickness=0,
|
2022-05-31 23:32:21 +03:00
|
|
|
width=self.apply_widget_scaling(self._desired_width),
|
|
|
|
height=self.apply_widget_scaling(self._desired_height))
|
2022-05-02 00:29:14 +03:00
|
|
|
self.canvas.grid(column=0, row=0, rowspan=1, columnspan=1, sticky="nswe")
|
2022-05-22 01:02:45 +03:00
|
|
|
self.draw_engine = DrawEngine(self.canvas)
|
2022-03-01 18:12:55 +03:00
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
self.canvas.bind("<Enter>", self.on_enter)
|
|
|
|
self.canvas.bind("<Leave>", self.on_leave)
|
|
|
|
self.canvas.bind("<Button-1>", self.clicked)
|
|
|
|
self.canvas.bind("<B1-Motion>", self.clicked)
|
|
|
|
|
2022-01-23 19:06:34 +03:00
|
|
|
# Each time an item is resized due to pack position mode, the binding Configure is called on the widget
|
2022-04-21 19:34:58 +03:00
|
|
|
self.bind('<Configure>', self.update_dimensions_event)
|
2022-01-23 19:06:34 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
self.set_cursor()
|
2022-01-16 18:54:21 +03:00
|
|
|
self.draw() # initial draw
|
|
|
|
|
|
|
|
if self.variable is not None:
|
2022-02-28 02:35:52 +03:00
|
|
|
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
2022-01-16 18:54:21 +03:00
|
|
|
self.variable_callback_blocked = True
|
|
|
|
self.set(self.variable.get(), from_variable_callback=True)
|
|
|
|
self.variable_callback_blocked = False
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-05-02 00:29:14 +03:00
|
|
|
def set_scaling(self, *args, **kwargs):
|
|
|
|
super().set_scaling(*args, **kwargs)
|
|
|
|
|
2022-05-31 23:32:21 +03:00
|
|
|
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width), height=self.apply_widget_scaling(self._desired_height))
|
2022-05-02 00:29:14 +03:00
|
|
|
self.draw()
|
|
|
|
|
2022-05-24 02:00:58 +03:00
|
|
|
def set_dimensions(self, width=None, height=None):
|
|
|
|
super().set_dimensions(width, height)
|
|
|
|
|
2022-05-31 23:32:21 +03:00
|
|
|
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
|
|
|
height=self.apply_widget_scaling(self._desired_height))
|
2022-05-24 02:00:58 +03:00
|
|
|
self.draw()
|
|
|
|
|
2022-01-02 00:16:06 +03:00
|
|
|
def destroy(self):
|
2022-02-28 02:35:52 +03:00
|
|
|
# remove variable_callback from variable callbacks if variable exists
|
2022-01-16 18:54:21 +03:00
|
|
|
if self.variable is not None:
|
2022-02-28 02:35:52 +03:00
|
|
|
self.variable.trace_remove("write", self.variable_callback_name)
|
2022-01-16 18:54:21 +03:00
|
|
|
|
2022-01-02 00:16:06 +03:00
|
|
|
super().destroy()
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
def set_cursor(self):
|
2022-06-15 03:07:51 +03:00
|
|
|
if self.state == "normal" and Settings.cursor_manipulation_enabled:
|
2022-05-02 00:29:14 +03:00
|
|
|
if sys.platform == "darwin":
|
|
|
|
self.configure(cursor="pointinghand")
|
|
|
|
elif sys.platform.startswith("win"):
|
|
|
|
self.configure(cursor="hand2")
|
2022-03-01 18:12:55 +03:00
|
|
|
|
2022-06-15 03:07:51 +03:00
|
|
|
elif self.state == "disabled" and Settings.cursor_manipulation_enabled:
|
|
|
|
if sys.platform == "darwin":
|
|
|
|
self.configure(cursor="arrow")
|
|
|
|
elif sys.platform.startswith("win"):
|
|
|
|
self.configure(cursor="arrow")
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
def draw(self, no_color_updates=False):
|
2022-06-19 22:16:19 +03:00
|
|
|
if self.orientation.lower() == "horizontal":
|
2022-05-16 17:51:19 +03:00
|
|
|
orientation = "w"
|
2022-06-19 22:16:19 +03:00
|
|
|
elif self.orientation.lower() == "vertical":
|
2022-05-16 17:51:19 +03:00
|
|
|
orientation = "s"
|
|
|
|
else:
|
|
|
|
orientation = "w"
|
|
|
|
|
2022-05-31 23:32:21 +03:00
|
|
|
requires_recoloring = self.draw_engine.draw_rounded_slider_with_border_and_button(self.apply_widget_scaling(self._current_width),
|
|
|
|
self.apply_widget_scaling(self._current_height),
|
2022-05-02 00:29:14 +03:00
|
|
|
self.apply_widget_scaling(self.corner_radius),
|
|
|
|
self.apply_widget_scaling(self.border_width),
|
|
|
|
self.apply_widget_scaling(self.button_length),
|
|
|
|
self.apply_widget_scaling(self.button_corner_radius),
|
2022-05-16 17:51:19 +03:00
|
|
|
self.value, orientation)
|
2021-11-11 00:17:49 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
if no_color_updates is False or requires_recoloring:
|
2022-05-31 23:32:21 +03:00
|
|
|
self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
2022-01-07 22:54:45 +03:00
|
|
|
|
|
|
|
if self.border_color is None:
|
2022-05-31 23:32:21 +03:00
|
|
|
self.canvas.itemconfig("border_parts", fill=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
|
|
|
outline=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
2022-01-07 22:54:45 +03:00
|
|
|
else:
|
2022-05-31 23:32:21 +03:00
|
|
|
self.canvas.itemconfig("border_parts", fill=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
|
|
|
outline=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
2022-01-07 22:54:45 +03:00
|
|
|
|
2022-05-31 23:32:21 +03:00
|
|
|
self.canvas.itemconfig("inner_parts", fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
|
|
|
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
2022-01-07 22:54:45 +03:00
|
|
|
|
|
|
|
if self.progress_color is None:
|
2022-05-31 23:32:21 +03:00
|
|
|
self.canvas.itemconfig("progress_parts", fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
|
|
|
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
2022-01-07 22:54:45 +03:00
|
|
|
else:
|
2022-05-31 23:32:21 +03:00
|
|
|
self.canvas.itemconfig("progress_parts", fill=ThemeManager.single_color(self.progress_color, self._appearance_mode),
|
|
|
|
outline=ThemeManager.single_color(self.progress_color, self._appearance_mode))
|
2022-01-07 22:54:45 +03:00
|
|
|
|
2022-06-15 03:07:51 +03:00
|
|
|
if self.hover_state is True:
|
|
|
|
self.canvas.itemconfig("slider_parts",
|
|
|
|
fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
|
|
|
|
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode))
|
|
|
|
else:
|
|
|
|
self.canvas.itemconfig("slider_parts",
|
|
|
|
fill=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
|
|
|
outline=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
2021-11-11 00:17:49 +03:00
|
|
|
|
2021-11-10 22:54:53 +03:00
|
|
|
def clicked(self, event=None):
|
2022-06-15 03:07:51 +03:00
|
|
|
if self.state == "normal":
|
2022-06-19 22:16:19 +03:00
|
|
|
if self.orientation.lower() == "horizontal":
|
2022-06-15 03:07:51 +03:00
|
|
|
self.value = (event.x / self._current_width) / self._widget_scaling
|
|
|
|
else:
|
|
|
|
self.value = 1 - (event.y / self._current_height) / self._widget_scaling
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-06-15 03:07:51 +03:00
|
|
|
if self.value > 1:
|
|
|
|
self.value = 1
|
|
|
|
if self.value < 0:
|
|
|
|
self.value = 0
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-06-15 03:07:51 +03:00
|
|
|
self.output_value = self.round_to_step_size(self.from_ + (self.value * (self.to - self.from_)))
|
|
|
|
self.value = (self.output_value - self.from_) / (self.to - self.from_)
|
2021-09-13 15:27:29 +03:00
|
|
|
|
2022-06-15 03:07:51 +03:00
|
|
|
self.draw(no_color_updates=False)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-06-15 03:07:51 +03:00
|
|
|
if self.variable is not None:
|
|
|
|
self.variable_callback_blocked = True
|
|
|
|
self.variable.set(round(self.output_value) if isinstance(self.variable, tkinter.IntVar) else self.output_value)
|
|
|
|
self.variable_callback_blocked = False
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-07-05 15:39:12 +03:00
|
|
|
if self.command is not None:
|
|
|
|
self.command(self.output_value)
|
2022-01-16 18:54:21 +03:00
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
def on_enter(self, event=0):
|
2022-06-15 03:07:51 +03:00
|
|
|
if self.state == "normal":
|
|
|
|
self.hover_state = True
|
|
|
|
self.canvas.itemconfig("slider_parts",
|
|
|
|
fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
|
|
|
|
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode))
|
2021-03-04 20:27:46 +03:00
|
|
|
|
|
|
|
def on_leave(self, event=0):
|
|
|
|
self.hover_state = False
|
2022-06-15 03:07:51 +03:00
|
|
|
self.canvas.itemconfig("slider_parts",
|
|
|
|
fill=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
2022-05-31 23:32:21 +03:00
|
|
|
outline=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2021-12-20 13:55:07 +03:00
|
|
|
def round_to_step_size(self, value):
|
|
|
|
if self.number_of_steps is not None:
|
|
|
|
step_size = (self.to - self.from_) / self.number_of_steps
|
|
|
|
value = self.to - (round((self.to - value) / step_size) * step_size)
|
|
|
|
return value
|
|
|
|
else:
|
|
|
|
return value
|
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
def get(self):
|
2021-09-13 15:27:29 +03:00
|
|
|
return self.output_value
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-16 18:54:21 +03:00
|
|
|
def set(self, output_value, from_variable_callback=False):
|
2022-02-23 03:47:34 +03:00
|
|
|
if self.from_ < self.to:
|
|
|
|
if output_value > self.to:
|
|
|
|
output_value = self.to
|
|
|
|
elif output_value < self.from_:
|
|
|
|
output_value = self.from_
|
|
|
|
else:
|
|
|
|
if output_value < self.to:
|
|
|
|
output_value = self.to
|
|
|
|
elif output_value > self.from_:
|
|
|
|
output_value = self.from_
|
2021-12-22 12:48:41 +03:00
|
|
|
|
2021-12-20 13:55:07 +03:00
|
|
|
self.output_value = self.round_to_step_size(output_value)
|
2021-09-13 15:27:29 +03:00
|
|
|
self.value = (self.output_value - self.from_) / (self.to - self.from_)
|
2021-11-11 00:17:49 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
self.draw(no_color_updates=False)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-16 18:54:21 +03:00
|
|
|
if self.variable is not None and not from_variable_callback:
|
|
|
|
self.variable_callback_blocked = True
|
|
|
|
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):
|
|
|
|
if not self.variable_callback_blocked:
|
|
|
|
self.set(self.variable.get(), from_variable_callback=True)
|
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
def configure(self, require_redraw=False, **kwargs):
|
2022-06-15 03:07:51 +03:00
|
|
|
if "state" in kwargs:
|
|
|
|
self.state = kwargs["state"]
|
|
|
|
self.set_cursor()
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["state"]
|
|
|
|
|
2021-12-22 12:48:41 +03:00
|
|
|
if "fg_color" in kwargs:
|
|
|
|
self.fg_color = kwargs["fg_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["fg_color"]
|
|
|
|
|
|
|
|
if "progress_color" in kwargs:
|
2021-12-31 02:22:32 +03:00
|
|
|
if kwargs["progress_color"] is None:
|
|
|
|
self.progress_color = self.fg_color
|
|
|
|
else:
|
|
|
|
self.progress_color = kwargs["progress_color"]
|
2021-12-22 12:48:41 +03:00
|
|
|
require_redraw = True
|
|
|
|
del kwargs["progress_color"]
|
|
|
|
|
|
|
|
if "button_color" in kwargs:
|
|
|
|
self.button_color = kwargs["button_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["button_color"]
|
|
|
|
|
|
|
|
if "button_hover_color" in kwargs:
|
|
|
|
self.button_hover_color = kwargs["button_hover_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["button_hover_color"]
|
|
|
|
|
|
|
|
if "border_color" in kwargs:
|
|
|
|
self.border_color = kwargs["border_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["border_color"]
|
|
|
|
|
|
|
|
if "border_width" in kwargs:
|
|
|
|
self.border_width = kwargs["border_width"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["border_width"]
|
|
|
|
|
|
|
|
if "from_" in kwargs:
|
|
|
|
self.from_ = kwargs["from_"]
|
|
|
|
del kwargs["from_"]
|
|
|
|
|
|
|
|
if "to" in kwargs:
|
|
|
|
self.to = kwargs["to"]
|
|
|
|
del kwargs["to"]
|
|
|
|
|
|
|
|
if "number_of_steps" in kwargs:
|
|
|
|
self.number_of_steps = kwargs["number_of_steps"]
|
|
|
|
del kwargs["number_of_steps"]
|
|
|
|
|
|
|
|
if "command" in kwargs:
|
2022-07-05 15:39:12 +03:00
|
|
|
self.command = kwargs["command"]
|
2021-12-22 12:48:41 +03:00
|
|
|
del kwargs["command"]
|
|
|
|
|
2022-01-16 18:54:21 +03:00
|
|
|
if "variable" in kwargs:
|
|
|
|
if self.variable is not None:
|
2022-02-28 02:35:52 +03:00
|
|
|
self.variable.trace_remove("write", self.variable_callback_name)
|
2022-01-16 18:54:21 +03:00
|
|
|
|
|
|
|
self.variable = kwargs["variable"]
|
|
|
|
|
|
|
|
if self.variable is not None and self.variable != "":
|
2022-02-28 02:35:52 +03:00
|
|
|
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
2022-01-16 18:54:21 +03:00
|
|
|
self.set(self.variable.get(), from_variable_callback=True)
|
|
|
|
else:
|
|
|
|
self.variable = None
|
|
|
|
|
|
|
|
del kwargs["variable"]
|
|
|
|
|
2022-05-24 02:00:58 +03:00
|
|
|
if "width" in kwargs:
|
|
|
|
self.set_dimensions(width=kwargs["width"])
|
|
|
|
del kwargs["width"]
|
|
|
|
|
|
|
|
if "height" in kwargs:
|
|
|
|
self.set_dimensions(height=kwargs["height"])
|
|
|
|
del kwargs["height"]
|
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
super().configure(require_redraw=require_redraw, **kwargs)
|