2021-03-04 20:27:46 +03:00
|
|
|
import tkinter
|
2022-09-17 01:18:31 +03:00
|
|
|
import math
|
2022-11-11 01:27:55 +03:00
|
|
|
from typing import Union, Tuple, Optional
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-10-23 23:33:57 +03:00
|
|
|
from .core_rendering.ctk_canvas import CTkCanvas
|
2022-10-29 14:11:55 +03:00
|
|
|
from .theme.theme_manager import ThemeManager
|
2022-10-23 23:33:57 +03:00
|
|
|
from .core_rendering.draw_engine import DrawEngine
|
2022-10-29 01:42:33 +03:00
|
|
|
from .core_widget_classes.widget_base_class import CTkBaseClass
|
2021-03-04 20:27:46 +03:00
|
|
|
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
class CTkProgressBar(CTkBaseClass):
|
2022-10-02 04:23:10 +03:00
|
|
|
"""
|
|
|
|
Progressbar with rounded corners, border, variable support,
|
|
|
|
indeterminate mode, vertical orientation.
|
|
|
|
For detailed information check out the documentation.
|
|
|
|
"""
|
2021-09-13 15:27:29 +03:00
|
|
|
|
2022-10-06 16:53:52 +03:00
|
|
|
def __init__(self,
|
2022-11-11 01:27:55 +03:00
|
|
|
master: any,
|
|
|
|
width: Optional[int] = None,
|
|
|
|
height: Optional[int] = None,
|
|
|
|
corner_radius: Optional[int] = None,
|
|
|
|
border_width: Optional[int] = None,
|
|
|
|
|
|
|
|
bg_color: Union[str, Tuple[str, str]] = "transparent",
|
|
|
|
fg_color: Optional[Union[str, Tuple[str, str]]] = None,
|
|
|
|
border_color: Optional[Union[str, Tuple[str, str]]] = None,
|
|
|
|
progress_color: Optional[Union[str, Tuple[str, str]]] = None,
|
|
|
|
|
|
|
|
variable: Union[tkinter.Variable, None] = None,
|
2022-10-03 01:33:06 +03:00
|
|
|
orientation: str = "horizontal",
|
2022-10-02 04:23:10 +03:00
|
|
|
mode: str = "determinate",
|
|
|
|
determinate_speed: float = 1,
|
|
|
|
indeterminate_speed: float = 1,
|
2022-01-16 18:54:21 +03:00
|
|
|
**kwargs):
|
2022-04-20 23:50:57 +03:00
|
|
|
|
2022-05-16 17:51:19 +03:00
|
|
|
# set default dimensions according to orientation
|
2022-11-11 01:27:55 +03:00
|
|
|
if width is None:
|
2022-10-03 01:33:06 +03:00
|
|
|
if orientation.lower() == "vertical":
|
2022-05-16 17:51:19 +03:00
|
|
|
width = 8
|
|
|
|
else:
|
|
|
|
width = 200
|
2022-11-11 01:27:55 +03:00
|
|
|
if height is None:
|
2022-10-03 01:33:06 +03:00
|
|
|
if orientation.lower() == "vertical":
|
2022-05-16 17:51:19 +03:00
|
|
|
height = 200
|
|
|
|
else:
|
|
|
|
height = 8
|
|
|
|
|
2022-11-01 02:37:30 +03:00
|
|
|
# transfer basic functionality (_bg_color, size, __appearance_mode, scaling) to CTkBaseClass
|
2022-10-06 16:53:52 +03:00
|
|
|
super().__init__(master=master, bg_color=bg_color, width=width, height=height, **kwargs)
|
2022-04-20 23:50:57 +03:00
|
|
|
|
|
|
|
# color
|
2022-11-27 04:48:09 +03:00
|
|
|
self._border_color = ThemeManager.theme["CTkProgressBar"]["border_color"] if border_color is None else self._check_color_type(border_color)
|
|
|
|
self._fg_color = ThemeManager.theme["CTkProgressBar"]["fg_color"] if fg_color is None else self._check_color_type(fg_color)
|
|
|
|
self._progress_color = ThemeManager.theme["CTkProgressBar"]["progress_color"] if progress_color is None else self._check_color_type(progress_color)
|
2021-08-25 18:02:16 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# control variable
|
2022-10-02 04:23:10 +03:00
|
|
|
self._variable = variable
|
|
|
|
self._variable_callback_blocked = False
|
|
|
|
self._variable_callback_name = None
|
2022-01-16 18:54:21 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# shape
|
2022-11-27 04:48:09 +03:00
|
|
|
self._corner_radius = ThemeManager.theme["CTkProgressBar"]["corner_radius"] if corner_radius is None else corner_radius
|
|
|
|
self._border_width = ThemeManager.theme["CTkProgressBar"]["border_width"] if border_width is None else border_width
|
2022-10-02 04:23:10 +03:00
|
|
|
self._determinate_value: float = 0.5 # range 0-1
|
|
|
|
self._determinate_speed = determinate_speed # range 0-1
|
|
|
|
self._indeterminate_value: float = 0 # range 0-inf
|
|
|
|
self._indeterminate_width: float = 0.4 # range 0-1
|
|
|
|
self._indeterminate_speed = indeterminate_speed # range 0-1 to travel in 50ms
|
|
|
|
self._loop_running: bool = False
|
2022-10-03 01:33:06 +03:00
|
|
|
self._orientation = orientation
|
2022-10-02 04:23:10 +03:00
|
|
|
self._mode = mode # "determinate" or "indeterminate"
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-05-02 00:29:14 +03:00
|
|
|
self.grid_rowconfigure(0, weight=1)
|
|
|
|
self.grid_columnconfigure(0, weight=1)
|
2022-04-21 19:34:58 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas = CTkCanvas(master=self,
|
|
|
|
highlightthickness=0,
|
|
|
|
width=self._apply_widget_scaling(self._desired_width),
|
|
|
|
height=self._apply_widget_scaling(self._desired_height))
|
|
|
|
self._canvas.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="nswe")
|
|
|
|
self._draw_engine = DrawEngine(self._canvas)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._draw() # initial draw
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._variable is not None:
|
|
|
|
self._variable_callback_name = self._variable.trace_add("write", self._variable_callback)
|
|
|
|
self._variable_callback_blocked = True
|
|
|
|
self.set(self._variable.get(), from_variable_callback=True)
|
|
|
|
self._variable_callback_blocked = False
|
2021-11-10 22:54:53 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _set_scaling(self, *args, **kwargs):
|
|
|
|
super()._set_scaling(*args, **kwargs)
|
2022-05-02 00:29:14 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
|
|
|
|
height=self._apply_widget_scaling(self._desired_height))
|
2022-10-15 02:02:54 +03:00
|
|
|
self._draw(no_color_updates=True)
|
2022-05-02 00:29:14 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _set_dimensions(self, width=None, height=None):
|
|
|
|
super()._set_dimensions(width, height)
|
2022-05-24 02:00:58 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
|
|
|
|
height=self._apply_widget_scaling(self._desired_height))
|
|
|
|
self._draw()
|
2022-05-24 02:00:58 +03:00
|
|
|
|
2022-01-02 00:16:06 +03:00
|
|
|
def destroy(self):
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._variable is not None:
|
|
|
|
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-10-02 04:23:10 +03:00
|
|
|
def _draw(self, no_color_updates=False):
|
2022-11-01 02:37:30 +03:00
|
|
|
super()._draw(no_color_updates)
|
|
|
|
|
2022-10-03 01:33:06 +03:00
|
|
|
if self._orientation.lower() == "horizontal":
|
2022-05-16 17:51:19 +03:00
|
|
|
orientation = "w"
|
2022-10-03 01:33:06 +03:00
|
|
|
elif self._orientation.lower() == "vertical":
|
2022-05-16 17:51:19 +03:00
|
|
|
orientation = "s"
|
|
|
|
else:
|
|
|
|
orientation = "w"
|
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._mode == "determinate":
|
|
|
|
requires_recoloring = self._draw_engine.draw_rounded_progress_bar_with_border(self._apply_widget_scaling(self._current_width),
|
|
|
|
self._apply_widget_scaling(self._current_height),
|
|
|
|
self._apply_widget_scaling(self._corner_radius),
|
|
|
|
self._apply_widget_scaling(self._border_width),
|
|
|
|
0,
|
|
|
|
self._determinate_value,
|
|
|
|
orientation)
|
2022-09-17 01:18:31 +03:00
|
|
|
else: # indeterminate mode
|
2022-10-02 04:23:10 +03:00
|
|
|
progress_value = (math.sin(self._indeterminate_value * math.pi / 40) + 1) / 2
|
|
|
|
progress_value_1 = min(1.0, progress_value + (self._indeterminate_width / 2))
|
|
|
|
progress_value_2 = max(0.0, progress_value - (self._indeterminate_width / 2))
|
|
|
|
|
|
|
|
requires_recoloring = self._draw_engine.draw_rounded_progress_bar_with_border(self._apply_widget_scaling(self._current_width),
|
|
|
|
self._apply_widget_scaling(self._current_height),
|
|
|
|
self._apply_widget_scaling(self._corner_radius),
|
|
|
|
self._apply_widget_scaling(self._border_width),
|
|
|
|
progress_value_1,
|
|
|
|
progress_value_2,
|
|
|
|
orientation)
|
2021-11-10 22:54:53 +03:00
|
|
|
|
2022-02-28 02:35:52 +03:00
|
|
|
if no_color_updates is False or requires_recoloring:
|
2022-10-29 02:20:32 +03:00
|
|
|
self._canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.itemconfig("border_parts",
|
2022-10-29 02:20:32 +03:00
|
|
|
fill=self._apply_appearance_mode(self._border_color),
|
|
|
|
outline=self._apply_appearance_mode(self._border_color))
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.itemconfig("inner_parts",
|
2022-10-29 02:20:32 +03:00
|
|
|
fill=self._apply_appearance_mode(self._fg_color),
|
|
|
|
outline=self._apply_appearance_mode(self._fg_color))
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.itemconfig("progress_parts",
|
2022-10-29 02:20:32 +03:00
|
|
|
fill=self._apply_appearance_mode(self._progress_color),
|
|
|
|
outline=self._apply_appearance_mode(self._progress_color))
|
2021-11-10 22:54:53 +03:00
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
def configure(self, require_redraw=False, **kwargs):
|
2022-11-06 16:40:15 +03:00
|
|
|
if "corner_radius" in kwargs:
|
|
|
|
self._corner_radius = kwargs.pop("corner_radius")
|
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "border_width" in kwargs:
|
|
|
|
self._border_width = kwargs.pop("border_width")
|
|
|
|
require_redraw = True
|
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
if "fg_color" in kwargs:
|
2022-11-11 01:27:55 +03:00
|
|
|
self._fg_color = self._check_color_type(kwargs.pop("fg_color"))
|
2022-01-07 22:54:45 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "border_color" in kwargs:
|
2022-11-11 01:27:55 +03:00
|
|
|
self._border_color = self._check_color_type(kwargs.pop("border_color"))
|
2022-01-07 22:54:45 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "progress_color" in kwargs:
|
2022-11-11 01:27:55 +03:00
|
|
|
self._progress_color = self._check_color_type(kwargs.pop("progress_color"))
|
2022-01-07 22:54:45 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-01-16 18:54:21 +03:00
|
|
|
if "variable" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._variable is not None:
|
|
|
|
self._variable.trace_remove("write", self._variable_callback_name)
|
2022-01-16 18:54:21 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._variable = kwargs.pop("variable")
|
2022-01-16 18:54:21 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._variable is not None and self._variable != "":
|
|
|
|
self._variable_callback_name = self._variable.trace_add("write", self._variable_callback)
|
|
|
|
self.set(self._variable.get(), from_variable_callback=True)
|
2022-01-16 18:54:21 +03:00
|
|
|
else:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._variable = None
|
2022-01-16 18:54:21 +03:00
|
|
|
|
2022-09-17 01:18:31 +03:00
|
|
|
if "mode" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._mode = kwargs.pop("mode")
|
2022-09-17 01:18:31 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-09-17 01:39:27 +03:00
|
|
|
if "determinate_speed" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._determinate_speed = kwargs.pop("determinate_speed")
|
2022-09-17 01:39:27 +03:00
|
|
|
|
|
|
|
if "indeterminate_speed" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._indeterminate_speed = kwargs.pop("indeterminate_speed")
|
2022-09-17 01:39:27 +03:00
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
super().configure(require_redraw=require_redraw, **kwargs)
|
2022-01-07 22:54:45 +03:00
|
|
|
|
2022-10-03 01:33:06 +03:00
|
|
|
def cget(self, attribute_name: str) -> any:
|
|
|
|
if attribute_name == "corner_radius":
|
|
|
|
return self._corner_radius
|
|
|
|
elif attribute_name == "border_width":
|
|
|
|
return self._border_width
|
|
|
|
|
|
|
|
elif attribute_name == "fg_color":
|
|
|
|
return self._fg_color
|
|
|
|
elif attribute_name == "border_color":
|
|
|
|
return self._border_color
|
|
|
|
elif attribute_name == "progress_color":
|
|
|
|
return self._progress_color
|
|
|
|
|
|
|
|
elif attribute_name == "variable":
|
|
|
|
return self._variable
|
|
|
|
elif attribute_name == "orientation":
|
|
|
|
return self._orientation
|
|
|
|
elif attribute_name == "mode":
|
|
|
|
return self._mode
|
|
|
|
elif attribute_name == "determinate_speed":
|
|
|
|
return self._determinate_speed
|
|
|
|
elif attribute_name == "indeterminate_speed":
|
|
|
|
return self._indeterminate_speed
|
2022-10-04 04:03:43 +03:00
|
|
|
|
2022-10-03 01:33:06 +03:00
|
|
|
else:
|
|
|
|
return super().cget(attribute_name)
|
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def _variable_callback(self, var_name, index, mode):
|
|
|
|
if not self._variable_callback_blocked:
|
|
|
|
self.set(self._variable.get(), from_variable_callback=True)
|
2022-01-16 18:54:21 +03:00
|
|
|
|
|
|
|
def set(self, value, from_variable_callback=False):
|
2022-09-17 01:18:31 +03:00
|
|
|
""" set determinate value """
|
2022-10-02 04:23:10 +03:00
|
|
|
self._determinate_value = value
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._determinate_value > 1:
|
|
|
|
self._determinate_value = 1
|
|
|
|
elif self._determinate_value < 0:
|
|
|
|
self._determinate_value = 0
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._draw(no_color_updates=True)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._variable is not None and not from_variable_callback:
|
|
|
|
self._variable_callback_blocked = True
|
|
|
|
self._variable.set(round(self._determinate_value) if isinstance(self._variable, tkinter.IntVar) else self._determinate_value)
|
|
|
|
self._variable_callback_blocked = False
|
2022-09-17 01:18:31 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
def get(self) -> float:
|
2022-09-17 01:18:31 +03:00
|
|
|
""" get determinate value """
|
2022-10-02 04:23:10 +03:00
|
|
|
return self._determinate_value
|
2022-09-17 01:18:31 +03:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
""" start indeterminate mode """
|
2022-10-02 04:23:10 +03:00
|
|
|
if not self._loop_running:
|
|
|
|
self._loop_running = True
|
|
|
|
self._internal_loop()
|
2022-09-17 01:18:31 +03:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
""" stop indeterminate mode """
|
2022-10-02 04:23:10 +03:00
|
|
|
self._loop_running = False
|
|
|
|
|
|
|
|
def _internal_loop(self):
|
|
|
|
if self._loop_running:
|
|
|
|
if self._mode == "determinate":
|
|
|
|
self._determinate_value += self._determinate_speed / 50
|
|
|
|
if self._determinate_value > 1:
|
|
|
|
self._determinate_value -= 1
|
|
|
|
self._draw()
|
|
|
|
self.after(20, self._internal_loop)
|
2022-09-17 01:18:31 +03:00
|
|
|
else:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._indeterminate_value += self._indeterminate_speed
|
|
|
|
self._draw()
|
|
|
|
self.after(20, self._internal_loop)
|
2022-09-17 01:18:31 +03:00
|
|
|
|
|
|
|
def step(self):
|
2022-10-02 04:23:10 +03:00
|
|
|
if self._mode == "determinate":
|
|
|
|
self._determinate_value += self._determinate_speed / 50
|
|
|
|
if self._determinate_value > 1:
|
|
|
|
self._determinate_value -= 1
|
|
|
|
self._draw()
|
2022-09-17 01:18:31 +03:00
|
|
|
else:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._indeterminate_value += self._indeterminate_speed
|
|
|
|
self._draw()
|
2022-10-04 03:26:29 +03:00
|
|
|
|
|
|
|
def bind(self, sequence=None, command=None, add=None):
|
|
|
|
""" called on the tkinter.Canvas """
|
|
|
|
return self._canvas.bind(sequence, command, add)
|
2022-10-04 04:03:43 +03:00
|
|
|
|
|
|
|
def unbind(self, sequence, funcid=None):
|
|
|
|
""" called on the tkinter.Canvas """
|
2022-10-04 04:16:59 +03:00
|
|
|
return self._canvas.unbind(sequence, funcid)
|
2022-10-05 22:11:46 +03:00
|
|
|
|
|
|
|
def focus(self):
|
|
|
|
return self._canvas.focus()
|
|
|
|
|
|
|
|
def focus_set(self):
|
|
|
|
return self._canvas.focus_set()
|
|
|
|
|
|
|
|
def focus_force(self):
|
|
|
|
return self._canvas.focus_force()
|