2021-03-04 20:27:46 +03:00
|
|
|
import tkinter
|
2022-11-11 01:27:55 +03:00
|
|
|
from typing import Union, Tuple, Callable, 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
|
|
|
|
from .font.ctk_font import CTkFont
|
2022-11-03 15:48:31 +03:00
|
|
|
from .image.ctk_image import CTkImage
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-10-16 21:13:19 +03:00
|
|
|
from customtkinter.utility.utility_functions import pop_from_dict_by_set, check_kwargs_empty
|
2022-10-04 03:26:29 +03:00
|
|
|
|
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-11-03 15:48:31 +03:00
|
|
|
_valid_tk_label_attributes = {"cursor", "justify", "padx", "pady",
|
2022-11-11 02:06:25 +03:00
|
|
|
"textvariable", "state", "takefocus", "underline"}
|
2022-10-03 01:33:06 +03:00
|
|
|
|
2022-10-06 16:53:52 +03:00
|
|
|
def __init__(self,
|
2022-11-11 01:27:55 +03:00
|
|
|
master: any,
|
2022-11-06 16:40:15 +03:00
|
|
|
width: int = 0,
|
2022-10-03 01:33:06 +03:00
|
|
|
height: int = 28,
|
2022-11-11 01:27:55 +03:00
|
|
|
corner_radius: Optional[int] = None,
|
2022-10-03 01:33:06 +03:00
|
|
|
|
2022-11-11 01:27:55 +03:00
|
|
|
bg_color: Union[str, Tuple[str, str]] = "transparent",
|
|
|
|
fg_color: Optional[Union[str, Tuple[str, str]]] = None,
|
|
|
|
text_color: Optional[Union[str, Tuple[str, str]]] = None,
|
2022-10-03 01:33:06 +03:00
|
|
|
|
2022-10-02 04:23:10 +03:00
|
|
|
text: str = "CTkLabel",
|
2022-11-11 01:27:55 +03:00
|
|
|
font: Optional[Union[tuple, CTkFont]] = None,
|
|
|
|
image: Union[tkinter.PhotoImage, CTkImage, None] = None,
|
2022-11-03 15:48:31 +03:00
|
|
|
compound: str = "center",
|
2022-10-02 04:23:10 +03:00
|
|
|
anchor: str = "center", # label anchor: center, n, e, s, w
|
2022-11-11 02:06:25 +03:00
|
|
|
wraplength: int = 0,
|
2021-03-04 20:27:46 +03:00
|
|
|
**kwargs):
|
2022-04-20 23:50:57 +03:00
|
|
|
|
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)
|
2022-04-20 23:50:57 +03:00
|
|
|
|
|
|
|
# color
|
2022-11-27 04:48:09 +03:00
|
|
|
self._fg_color = ThemeManager.theme["CTkLabel"]["fg_color"] if fg_color is None else self._check_color_type(fg_color, transparency=True)
|
|
|
|
self._text_color = ThemeManager.theme["CTkLabel"]["text_color"] if text_color is None else self._check_color_type(text_color)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# shape
|
2022-11-27 04:48:09 +03:00
|
|
|
self._corner_radius = ThemeManager.theme["CTkLabel"]["corner_radius"] if corner_radius is None 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-11-11 02:06:25 +03:00
|
|
|
self._wraplength = wraplength
|
2022-10-21 22:28:31 +03:00
|
|
|
|
2022-11-03 15:48:31 +03:00
|
|
|
# image
|
2022-11-06 16:40:15 +03:00
|
|
|
self._image = self._check_image_type(image)
|
2022-11-03 15:48:31 +03:00
|
|
|
self._compound = compound
|
|
|
|
if isinstance(self._image, CTkImage):
|
|
|
|
self._image.add_configure_callback(self._update_image)
|
|
|
|
|
2022-10-21 22:28:31 +03:00
|
|
|
# font
|
2022-11-11 01:27:55 +03:00
|
|
|
self._font = CTkFont() if font is None else self._check_font_type(font)
|
2022-10-21 22:28:31 +03:00
|
|
|
if isinstance(self._font, CTkFont):
|
|
|
|
self._font.add_size_configure_callback(self._update_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)
|
|
|
|
|
2022-11-03 15:48:31 +03:00
|
|
|
self._label = tkinter.Label(master=self,
|
|
|
|
highlightthickness=0,
|
|
|
|
padx=0,
|
|
|
|
pady=0,
|
|
|
|
borderwidth=0,
|
|
|
|
anchor=self._anchor,
|
|
|
|
compound=self._compound,
|
2022-11-11 02:06:25 +03:00
|
|
|
wraplength=self._apply_widget_scaling(self._wraplength),
|
2022-11-03 15:48:31 +03:00
|
|
|
text=self._text,
|
|
|
|
font=self._apply_font_scaling(self._font))
|
|
|
|
self._label.configure(**pop_from_dict_by_set(kwargs, self._valid_tk_label_attributes))
|
2022-10-04 03:26:29 +03:00
|
|
|
|
2022-10-16 21:13:19 +03:00
|
|
|
check_kwargs_empty(kwargs, raise_error=True)
|
2022-10-02 04:23:10 +03:00
|
|
|
|
2022-11-06 16:40:15 +03:00
|
|
|
self._create_grid()
|
2022-11-03 15:48:31 +03:00
|
|
|
self._update_image()
|
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-11-03 15:48:31 +03:00
|
|
|
self._label.configure(font=self._apply_font_scaling(self._font))
|
2022-11-11 02:06:25 +03:00
|
|
|
self._label.configure(wraplength=self._apply_widget_scaling(self._wraplength))
|
2022-10-04 03:26:29 +03:00
|
|
|
|
2022-11-06 16:40:15 +03:00
|
|
|
self._create_grid()
|
2022-10-15 02:02:54 +03:00
|
|
|
self._draw(no_color_updates=True)
|
2022-10-02 04:23:10 +03:00
|
|
|
|
|
|
|
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))
|
2022-11-06 16:40:15 +03:00
|
|
|
self._create_grid()
|
2022-10-02 04:23:10 +03:00
|
|
|
self._draw()
|
|
|
|
|
2022-10-21 22:28:31 +03:00
|
|
|
def _update_font(self):
|
|
|
|
""" pass font to tkinter widgets with applied font scaling and update grid with workaround """
|
2022-11-03 15:48:31 +03:00
|
|
|
self._label.configure(font=self._apply_font_scaling(self._font))
|
2022-10-21 22:28:31 +03:00
|
|
|
|
|
|
|
# Workaround to force grid to be resized when text changes size.
|
|
|
|
# Otherwise grid will lag and only resizes if other mouse action occurs.
|
|
|
|
self._canvas.grid_forget()
|
|
|
|
self._canvas.grid(row=0, column=0, sticky="nswe")
|
|
|
|
|
2022-11-03 15:48:31 +03:00
|
|
|
def _update_image(self):
|
|
|
|
if isinstance(self._image, CTkImage):
|
|
|
|
self._label.configure(image=self._image.create_scaled_photo_image(self._get_widget_scaling(),
|
|
|
|
self._get_appearance_mode()))
|
|
|
|
elif self._image is not None:
|
|
|
|
self._label.configure(image=self._image)
|
|
|
|
|
2022-10-21 22:28:31 +03:00
|
|
|
def destroy(self):
|
|
|
|
if isinstance(self._font, CTkFont):
|
|
|
|
self._font.remove_size_configure_callback(self._update_font)
|
|
|
|
super().destroy()
|
|
|
|
|
2022-11-06 16:40:15 +03:00
|
|
|
def _create_grid(self):
|
|
|
|
""" configure grid system (1x1) """
|
|
|
|
|
|
|
|
text_label_grid_sticky = self._anchor if self._anchor != "center" else ""
|
|
|
|
self._label.grid(row=0, column=0, sticky=text_label_grid_sticky,
|
|
|
|
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height / 2))))
|
|
|
|
|
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-02 04:23:10 +03:00
|
|
|
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-11-11 01:27:55 +03:00
|
|
|
if self._apply_appearance_mode(self._fg_color) == "transparent":
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.itemconfig("inner_parts",
|
2022-11-11 01:27:55 +03:00
|
|
|
fill=self._apply_appearance_mode(self._bg_color),
|
|
|
|
outline=self._apply_appearance_mode(self._bg_color))
|
2022-05-13 00:56:00 +03:00
|
|
|
|
2022-11-03 15:48:31 +03:00
|
|
|
self._label.configure(fg=self._apply_appearance_mode(self._text_color),
|
2022-11-11 01:27:55 +03:00
|
|
|
bg=self._apply_appearance_mode(self._bg_color))
|
2022-05-13 00:56:00 +03:00
|
|
|
else:
|
2022-10-02 04:23:10 +03:00
|
|
|
self._canvas.itemconfig("inner_parts",
|
2022-11-11 01:27:55 +03:00
|
|
|
fill=self._apply_appearance_mode(self._fg_color),
|
|
|
|
outline=self._apply_appearance_mode(self._fg_color))
|
2022-05-13 00:56:00 +03:00
|
|
|
|
2022-11-03 15:48:31 +03:00
|
|
|
self._label.configure(fg=self._apply_appearance_mode(self._text_color),
|
2022-11-11 01:27:55 +03:00
|
|
|
bg=self._apply_appearance_mode(self._fg_color))
|
2022-05-13 00:56:00 +03:00
|
|
|
|
2022-10-29 02:20:32 +03:00
|
|
|
self._canvas.configure(bg=self._apply_appearance_mode(self._bg_color))
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-11-03 15:48:31 +03:00
|
|
|
if self._image is not None:
|
|
|
|
self._update_image()
|
|
|
|
|
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")
|
|
|
|
self._create_grid()
|
|
|
|
require_redraw = True
|
2022-11-03 15:48:31 +03:00
|
|
|
|
2022-11-06 16:40:15 +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"), transparency=True)
|
2022-11-06 16:40:15 +03:00
|
|
|
require_redraw = True
|
|
|
|
|
|
|
|
if "text_color" in kwargs:
|
2022-11-11 01:27:55 +03:00
|
|
|
self._text_color = self._check_color_type(kwargs.pop("text_color"))
|
2022-11-06 16:40:15 +03:00
|
|
|
require_redraw = True
|
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")
|
2022-11-03 15:48:31 +03:00
|
|
|
self._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:
|
2022-10-21 22:28:31 +03:00
|
|
|
if isinstance(self._font, CTkFont):
|
|
|
|
self._font.remove_size_configure_callback(self._update_font)
|
|
|
|
self._font = self._check_font_type(kwargs.pop("font"))
|
|
|
|
if isinstance(self._font, CTkFont):
|
|
|
|
self._font.add_size_configure_callback(self._update_font)
|
|
|
|
self._update_font()
|
2022-08-16 15:05:15 +03:00
|
|
|
|
2022-11-03 15:48:31 +03:00
|
|
|
if "image" in kwargs:
|
|
|
|
if isinstance(self._image, CTkImage):
|
|
|
|
self._image.remove_configure_callback(self._update_image)
|
2022-11-06 16:40:15 +03:00
|
|
|
self._image = self._check_image_type(kwargs.pop("image"))
|
2022-11-03 15:48:31 +03:00
|
|
|
if isinstance(self._image, CTkImage):
|
|
|
|
self._image.add_configure_callback(self._update_image)
|
|
|
|
self._update_image()
|
|
|
|
|
2022-11-06 16:40:15 +03:00
|
|
|
if "compound" in kwargs:
|
|
|
|
self._compound = kwargs.pop("compound")
|
|
|
|
self._label.configure(compound=self._compound)
|
2021-11-07 00:06:27 +03:00
|
|
|
|
2022-11-06 16:40:15 +03:00
|
|
|
if "anchor" in kwargs:
|
|
|
|
self._anchor = kwargs.pop("anchor")
|
|
|
|
self._label.configure(anchor=self._anchor)
|
|
|
|
self._create_grid()
|
2022-07-23 20:09:53 +03:00
|
|
|
|
2022-11-11 02:06:25 +03:00
|
|
|
if "wraplength" in kwargs:
|
|
|
|
self._wraplength = kwargs.pop("wraplength")
|
|
|
|
self._label.configure(wraplength=self._apply_widget_scaling(self._wraplength))
|
|
|
|
|
2022-11-03 15:48:31 +03:00
|
|
|
self._label.configure(**pop_from_dict_by_set(kwargs, self._valid_tk_label_attributes)) # configure tkinter.Label
|
2022-10-04 03:26:29 +03:00
|
|
|
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
|
2022-11-03 15:48:31 +03:00
|
|
|
elif attribute_name == "image":
|
|
|
|
return self._image
|
|
|
|
elif attribute_name == "compound":
|
|
|
|
return self._compound
|
2022-10-03 01:33:06 +03:00
|
|
|
elif attribute_name == "anchor":
|
|
|
|
return self._anchor
|
2022-11-11 02:06:25 +03:00
|
|
|
elif attribute_name == "wraplength":
|
|
|
|
return self._wraplength
|
2022-10-04 03:26:29 +03:00
|
|
|
|
|
|
|
elif attribute_name in self._valid_tk_label_attributes:
|
2022-11-03 15:48:31 +03:00
|
|
|
return self._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)
|
2022-11-03 15:48:31 +03:00
|
|
|
label_bind_return = self._label.bind(sequence, command, add)
|
2022-10-04 17:55:34 +03:00
|
|
|
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)
|
2022-11-03 15:48:31 +03:00
|
|
|
self._label.unbind(sequence, label_bind_return)
|
2022-10-05 22:11:46 +03:00
|
|
|
|
|
|
|
def focus(self):
|
2022-11-03 15:48:31 +03:00
|
|
|
return self._label.focus()
|
2022-10-05 22:11:46 +03:00
|
|
|
|
|
|
|
def focus_set(self):
|
2022-11-03 15:48:31 +03:00
|
|
|
return self._label.focus_set()
|
2022-10-05 22:11:46 +03:00
|
|
|
|
|
|
|
def focus_force(self):
|
2022-11-03 15:48:31 +03:00
|
|
|
return self._label.focus_force()
|