2022-07-12 22:52:52 +03:00
|
|
|
import sys
|
2021-03-04 20:27:46 +03:00
|
|
|
import tkinter
|
|
|
|
|
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-04-20 23:50:57 +03:00
|
|
|
class CTkLabel(CTkBaseClass):
|
2022-01-16 18:54:21 +03:00
|
|
|
def __init__(self, *args,
|
2021-03-04 20:27:46 +03:00
|
|
|
bg_color=None,
|
2022-02-23 00:38:40 +03:00
|
|
|
fg_color="default_theme",
|
|
|
|
text_color="default_theme",
|
2022-02-28 02:35:52 +03:00
|
|
|
corner_radius="default_theme",
|
2022-05-31 23:32:21 +03:00
|
|
|
width=140,
|
2022-05-25 23:14:38 +03:00
|
|
|
height=28,
|
2021-03-04 20:27:46 +03:00
|
|
|
text="CTkLabel",
|
2022-02-23 03:47:34 +03:00
|
|
|
text_font="default_theme",
|
2022-07-01 23:09:45 +03:00
|
|
|
anchor="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-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
|
|
|
if "master" in kwargs:
|
2022-07-07 17:02:51 +03:00
|
|
|
super().__init__(*args, bg_color=bg_color, width=width, height=height, master=kwargs.pop("master"))
|
2022-01-16 18:54:21 +03:00
|
|
|
else:
|
2022-04-20 23:50:57 +03:00
|
|
|
super().__init__(*args, bg_color=bg_color, width=width, height=height)
|
|
|
|
|
|
|
|
# color
|
2022-05-17 22:44:59 +03:00
|
|
|
self.fg_color = ThemeManager.theme["color"]["label"] if fg_color == "default_theme" else fg_color
|
2022-02-24 02:19:40 +03:00
|
|
|
if self.fg_color is None:
|
|
|
|
self.fg_color = self.bg_color
|
2022-05-17 22:44:59 +03:00
|
|
|
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-05-17 22:44:59 +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-07-01 23:09:45 +03:00
|
|
|
self.anchor = anchor
|
2021-03-04 20:27:46 +03:00
|
|
|
self.text = text
|
2022-05-17 22:44:59 +03:00
|
|
|
self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_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-02-24 02:19:40 +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-03-01 21:10:18 +03:00
|
|
|
self.canvas.grid(row=0, column=0, sticky="nswe")
|
2022-05-22 01:02:45 +03:00
|
|
|
self.draw_engine = DrawEngine(self.canvas)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
|
|
|
self.text_label = tkinter.Label(master=self,
|
2022-02-23 03:47:34 +03:00
|
|
|
highlightthickness=0,
|
2021-03-04 20:27:46 +03:00
|
|
|
bd=0,
|
2022-07-01 23:09:45 +03:00
|
|
|
anchor=self.anchor,
|
2021-03-04 20:27:46 +03:00
|
|
|
text=self.text,
|
2022-04-21 19:34:58 +03:00
|
|
|
font=self.apply_font_scaling(self.text_font),
|
2022-01-16 18:54:21 +03:00
|
|
|
**kwargs)
|
2022-07-01 23:09:45 +03:00
|
|
|
text_label_grid_sticky = self.anchor if self.anchor != "center" else ""
|
|
|
|
self.text_label.grid(row=0, column=0, padx=self.apply_widget_scaling(self.corner_radius),
|
|
|
|
sticky=text_label_grid_sticky)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-04-21 19:34:58 +03:00
|
|
|
self.bind('<Configure>', self.update_dimensions_event)
|
2021-03-04 20:27:46 +03:00
|
|
|
self.draw()
|
|
|
|
|
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.text_label.configure(font=self.apply_font_scaling(self.text_font))
|
2022-07-01 23:09:45 +03:00
|
|
|
text_label_grid_sticky = self.anchor if self.anchor != "center" else ""
|
|
|
|
self.text_label.grid(row=0, column=0, padx=self.apply_widget_scaling(self.corner_radius),
|
|
|
|
sticky=text_label_grid_sticky)
|
2022-05-02 00:29:14 +03:00
|
|
|
|
2022-05-22 20:56:18 +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-04-20 23:50:57 +03:00
|
|
|
def draw(self, no_color_updates=False):
|
2022-05-31 23:32:21 +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),
|
2022-05-02 00:29:14 +03:00
|
|
|
self.apply_widget_scaling(self.corner_radius),
|
2022-04-21 19:34:58 +03:00
|
|
|
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-05-31 23:32:21 +03:00
|
|
|
if ThemeManager.single_color(self.fg_color, self._appearance_mode) is not None:
|
2022-05-13 00:56:00 +03:00
|
|
|
self.canvas.itemconfig("inner_parts",
|
2022-05-31 23:32:21 +03:00
|
|
|
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-05-31 23:32:21 +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:
|
|
|
|
self.canvas.itemconfig("inner_parts",
|
2022-05-31 23:32:21 +03:00
|
|
|
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-05-31 23:32:21 +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-05-31 23:32:21 +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-12 22:52:52 +03:00
|
|
|
def config(self, **kwargs):
|
|
|
|
sys.stderr.write("Warning: Use .configure() instead of .config()")
|
|
|
|
self.configure(**kwargs)
|
|
|
|
|
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:
|
|
|
|
self.anchor = kwargs.pop("anchor")
|
|
|
|
text_label_grid_sticky = self.anchor if self.anchor != "center" else ""
|
|
|
|
self.text_label.grid(row=0, column=0, padx=self.apply_widget_scaling(self.corner_radius),
|
|
|
|
sticky=text_label_grid_sticky)
|
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
if "text" in kwargs:
|
2022-07-23 20:09:53 +03:00
|
|
|
self.text = kwargs["text"]
|
|
|
|
self.text_label.configure(text=self.text)
|
2021-11-07 00:06:27 +03:00
|
|
|
del kwargs["text"]
|
|
|
|
|
2022-08-16 15:05:15 +03:00
|
|
|
if "text_font" in kwargs:
|
|
|
|
self.text_font = kwargs.pop("text_font")
|
|
|
|
self.text_label.configure(font=self.apply_font_scaling(self.text_font))
|
|
|
|
|
2021-11-07 00:06:27 +03:00
|
|
|
if "fg_color" in kwargs:
|
|
|
|
self.fg_color = kwargs["fg_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["fg_color"]
|
|
|
|
|
|
|
|
if "text_color" in kwargs:
|
|
|
|
self.text_color = kwargs["text_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["text_color"]
|
|
|
|
|
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
|
|
|
if "bg_color" in kwargs:
|
|
|
|
super().configure(bg_color=kwargs.pop("bg_color"), require_redraw=require_redraw)
|
|
|
|
else:
|
|
|
|
super().configure(require_redraw=require_redraw)
|
2021-11-07 00:06:27 +03:00
|
|
|
|
2022-07-07 17:02:51 +03:00
|
|
|
self.text_label.configure(**kwargs) # pass remaining kwargs to label
|
2021-11-07 00:06:27 +03:00
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
def set_text(self, text):
|
2022-07-23 20:09:53 +03:00
|
|
|
""" Will be removed in the next major release """
|
|
|
|
|
2021-09-13 15:27:29 +03:00
|
|
|
self.text = text
|
2022-07-23 20:09:53 +03:00
|
|
|
self.text_label.configure(text=self.text)
|