2021-03-04 20:27:46 +03:00
|
|
|
import tkinter
|
2022-10-04 17:55:34 +03:00
|
|
|
from typing import Union, Tuple, Callable
|
2021-03-04 20:27:46 +03:00
|
|
|
|
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 ..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-10-04 03:26:29 +03:00
|
|
|
from .widget_helper_functions import pop_from_dict_by_set
|
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
class CTkLabel(CTkBaseClass):
|
2022-10-02 04:23:10 +03:00
|
|
|
"""
|
|
|
|
Label with rounded corners. Default is fg_color=None (transparent fg_color).
|
|
|
|
For detailed information check out the documentation.
|
|
|
|
"""
|
|
|
|
|
2022-10-03 01:33:06 +03:00
|
|
|
# attributes that are passed to and managed by the tkinter entry only:
|
2022-10-04 03:26:29 +03:00
|
|
|
_valid_tk_label_attributes = {"compound", "cursor", "image", "justify", "padx", "pady",
|
|
|
|
"textvariable", "state", "takefocus", "underline", "wraplength"}
|
2022-10-03 01:33:06 +03:00
|
|
|
|
2022-10-06 16:53:52 +03:00
|
|
|
def __init__(self,
|
|
|
|
master: any = None,
|
2022-10-03 01:33:06 +03:00
|
|
|
width: int = 140,
|
|
|
|
height: int = 28,
|
|
|
|
corner_radius: Union[int, str] = "default_theme",
|
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
bg_color: Union[str, Tuple[str, str], None] = None,
|
|
|
|
fg_color: Union[str, Tuple[str, str], None] = "default_theme",
|
|
|
|
text_color: Union[str, Tuple[str, str]] = "default_theme",
|
2022-10-03 01:33:06 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
text: str = "CTkLabel",
|
2022-10-03 01:33:06 +03:00
|
|
|
font: any = "default_theme",
|
2022-10-02 04:23:10 +03:00
|
|
|
anchor: str = "center", # label anchor: center, n, e, s, w
|
2021-03-04 20:27:46 +03:00
|
|
|
**kwargs):
|
2022-04-20 23:50:57 +03:00
|
|
|
|
2022-10-02 04:23:10 +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)
|
2022-04-20 23:50:57 +03:00
|
|
|
|
|
|
|
# color
|
2022-10-02 04:23:10 +03:00
|
|
|
self._fg_color = ThemeManager.theme["color"]["label"] if fg_color == "default_theme" else fg_color
|
|
|
|
self._text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# shape
|
2022-10-02 04:23:10 +03:00
|
|
|
self._corner_radius = ThemeManager.theme["shape"]["label_corner_radius"] if corner_radius == "default_theme" else corner_radius
|
2021-11-10 02:03:08 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# text
|
2022-10-02 04:23:10 +03:00
|
|
|
self._anchor = anchor
|
|
|
|
self._text = text
|
2022-10-03 01:33:06 +03:00
|
|
|
self._font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if font == "default_theme" else font
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# configure grid system (1x1)
|
2022-03-01 21:10:18 +03:00
|
|
|
self.grid_rowconfigure(0, weight=1)
|
|
|
|
self.grid_columnconfigure(0, weight=1)
|
|
|
|
|
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, sticky="nswe")
|
|
|
|
self._draw_engine = DrawEngine(self._canvas)
|
|
|
|
|
|
|
|
self._text_label = tkinter.Label(master=self,
|
|
|
|
highlightthickness=0,
|
|
|
|
bd=0,
|
|
|
|
anchor=self._anchor,
|
|
|
|
text=self._text,
|
2022-10-03 01:33:06 +03:00
|
|
|
font=self._apply_font_scaling(self._font),
|
2022-10-04 03:26:29 +03:00
|
|
|
**pop_from_dict_by_set(kwargs, self._valid_tk_label_attributes))
|
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
|
2022-10-04 03:26:29 +03:00
|
|
|
self._text_label.grid(row=0, column=0, sticky=text_label_grid_sticky,
|
2022-10-08 02:50:09 +03:00
|
|
|
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height/2))))
|
2022-10-04 03:26:29 +03:00
|
|
|
|
|
|
|
self._check_kwargs_empty(kwargs, raise_error=True)
|
2022-10-02 04:23:10 +03:00
|
|
|
|
2022-10-04 04:03:43 +03:00
|
|
|
super().bind('<Configure>', self._update_dimensions_event)
|
2022-10-02 04:23:10 +03:00
|
|
|
self._draw()
|
|
|
|
|
|
|
|
def _set_scaling(self, *args, **kwargs):
|
|
|
|
super()._set_scaling(*args, **kwargs)
|
|
|
|
|
|
|
|
self._canvas.configure(width=self._apply_widget_scaling(self._desired_width), height=self._apply_widget_scaling(self._desired_height))
|
2022-10-03 01:33:06 +03:00
|
|
|
self._text_label.configure(font=self._apply_font_scaling(self._font))
|
2022-10-04 03:26:29 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
|
2022-10-04 03:26:29 +03:00
|
|
|
self._text_label.grid(row=0, column=0, sticky=text_label_grid_sticky,
|
2022-10-08 02:50:09 +03:00
|
|
|
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height/2))))
|
2022-10-02 04:23:10 +03:00
|
|
|
|
|
|
|
self._draw()
|
|
|
|
|
|
|
|
def _set_dimensions(self, width=None, height=None):
|
|
|
|
super()._set_dimensions(width, height)
|
|
|
|
|
|
|
|
self._canvas.configure(width=self._apply_widget_scaling(self._desired_width),
|
|
|
|
height=self._apply_widget_scaling(self._desired_height))
|
|
|
|
self._draw()
|
|
|
|
|
|
|
|
def _draw(self, no_color_updates=False):
|
|
|
|
requires_recoloring = self._draw_engine.draw_rounded_rect_with_border(self._apply_widget_scaling(self._current_width),
|
|
|
|
self._apply_widget_scaling(self._current_height),
|
|
|
|
self._apply_widget_scaling(self._corner_radius),
|
|
|
|
0)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-05-13 00:56:00 +03:00
|
|
|
if no_color_updates is False or requires_recoloring:
|
2022-10-02 04:23:10 +03:00
|
|
|
if ThemeManager.single_color(self._fg_color, self._appearance_mode) is not None:
|
|
|
|
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-05-13 00:56:00 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._text_label.configure(fg=ThemeManager.single_color(self._text_color, self._appearance_mode),
|
|
|
|
bg=ThemeManager.single_color(self._fg_color, self._appearance_mode))
|
2022-05-13 00:56:00 +03:00
|
|
|
else:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.itemconfig("inner_parts",
|
|
|
|
fill=ThemeManager.single_color(self._bg_color, self._appearance_mode),
|
|
|
|
outline=ThemeManager.single_color(self._bg_color, self._appearance_mode))
|
2022-05-13 00:56:00 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._text_label.configure(fg=ThemeManager.single_color(self._text_color, self._appearance_mode),
|
|
|
|
bg=ThemeManager.single_color(self._bg_color, self._appearance_mode))
|
2022-05-13 00:56:00 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.configure(bg=ThemeManager.single_color(self._bg_color, self._appearance_mode))
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
def configure(self, require_redraw=False, **kwargs):
|
2022-07-01 23:09:45 +03:00
|
|
|
if "anchor" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._anchor = kwargs.pop("anchor")
|
|
|
|
text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
|
2022-10-04 03:26:29 +03:00
|
|
|
self._text_label.grid(row=0, column=0, sticky=text_label_grid_sticky,
|
2022-10-08 02:50:09 +03:00
|
|
|
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height/2))))
|
2022-07-01 23:09:45 +03:00
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
if "text" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._text = kwargs.pop("text")
|
|
|
|
self._text_label.configure(text=self._text)
|
2021-11-07 00:06:27 +03:00
|
|
|
|
2022-10-03 01:33:06 +03:00
|
|
|
if "font" in kwargs:
|
|
|
|
self._font = kwargs.pop("font")
|
|
|
|
self._text_label.configure(font=self._apply_font_scaling(self._font))
|
2022-08-16 15:05:15 +03:00
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
if "fg_color" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._fg_color = kwargs.pop("fg_color")
|
2021-11-07 00:06:27 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "text_color" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._text_color = kwargs.pop("text_color")
|
2021-11-07 00:06:27 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
2022-05-24 02:00:58 +03:00
|
|
|
if "width" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._set_dimensions(width=kwargs.pop("width"))
|
2022-05-24 02:00:58 +03:00
|
|
|
|
|
|
|
if "height" in kwargs:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._set_dimensions(height=kwargs.pop("height"))
|
2021-11-07 00:06:27 +03:00
|
|
|
|
2022-10-04 03:26:29 +03:00
|
|
|
if "corner_radius" in kwargs:
|
|
|
|
self._corner_radius = kwargs.pop("corner_radius")
|
|
|
|
text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
|
|
|
|
self._text_label.grid(row=0, column=0, sticky=text_label_grid_sticky,
|
2022-10-08 02:50:09 +03:00
|
|
|
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height/2))))
|
2022-10-04 03:26:29 +03:00
|
|
|
require_redraw = True
|
2022-07-23 20:09:53 +03:00
|
|
|
|
2022-10-04 03:26:29 +03:00
|
|
|
self._text_label.configure(**pop_from_dict_by_set(kwargs, self._valid_tk_label_attributes)) # configure tkinter.Label
|
|
|
|
super().configure(require_redraw=require_redraw, **kwargs) # configure CTkBaseClass
|
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 == "fg_color":
|
|
|
|
return self._fg_color
|
|
|
|
elif attribute_name == "text_color":
|
|
|
|
return self._text_color
|
|
|
|
|
|
|
|
elif attribute_name == "text":
|
|
|
|
return self._text
|
|
|
|
elif attribute_name == "font":
|
|
|
|
return self._font
|
|
|
|
elif attribute_name == "anchor":
|
|
|
|
return self._anchor
|
2022-10-04 03:26:29 +03:00
|
|
|
|
|
|
|
elif attribute_name in self._valid_tk_label_attributes:
|
|
|
|
return self._text_label.cget(attribute_name) # cget of tkinter.Label
|
2022-10-03 01:33:06 +03:00
|
|
|
else:
|
2022-10-04 03:26:29 +03:00
|
|
|
return super().cget(attribute_name) # cget of CTkBaseClass
|
|
|
|
|
2022-10-04 17:55:34 +03:00
|
|
|
def bind(self, sequence: str = None, command: Callable = None, add: str = None) -> str:
|
|
|
|
""" called on the tkinter.Label and tkinter.Canvas """
|
|
|
|
canvas_bind_return = self._canvas.bind(sequence, command, add)
|
|
|
|
label_bind_return = self._text_label.bind(sequence, command, add)
|
|
|
|
return canvas_bind_return + " + " + label_bind_return
|
|
|
|
|
|
|
|
def unbind(self, sequence: str, funcid: str = None):
|
|
|
|
""" called on the tkinter.Label and tkinter.Canvas """
|
|
|
|
canvas_bind_return, label_bind_return = funcid.split(" + ")
|
|
|
|
self._canvas.unbind(sequence, canvas_bind_return)
|
|
|
|
self._text_label.unbind(sequence, label_bind_return)
|
2022-10-05 22:11:46 +03:00
|
|
|
|
|
|
|
def focus(self):
|
|
|
|
return self._text_label.focus()
|
|
|
|
|
|
|
|
def focus_set(self):
|
|
|
|
return self._text_label.focus_set()
|
|
|
|
|
|
|
|
def focus_force(self):
|
|
|
|
return self._text_label.focus_force()
|