CustomTkinter/customtkinter/widgets/ctk_label.py

160 lines
7.2 KiB
Python
Raw Normal View History

2022-07-12 22:52:52 +03:00
import sys
2021-03-04 20:27:46 +03:00
import tkinter
from .ctk_canvas import CTkCanvas
from ..theme_manager import ThemeManager
from ..draw_engine import DrawEngine
from .widget_base_class import CTkBaseClass
2021-03-04 20:27:46 +03:00
class CTkLabel(CTkBaseClass):
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",
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",
anchor="center", # label anchor: center, n, e, s, w
2021-03-04 20:27:46 +03:00
**kwargs):
2022-05-31 23:32:21 +03:00
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
if "master" in kwargs:
super().__init__(*args, bg_color=bg_color, width=width, height=height, master=kwargs.pop("master"))
else:
super().__init__(*args, bg_color=bg_color, width=width, height=height)
# color
self.fg_color = ThemeManager.theme["color"]["label"] if fg_color == "default_theme" else fg_color
if self.fg_color is None:
self.fg_color = self.bg_color
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
2021-03-04 20:27:46 +03:00
# shape
self.corner_radius = ThemeManager.theme["shape"]["label_corner_radius"] if corner_radius == "default_theme" else corner_radius
# text
self.anchor = anchor
2021-03-04 20:27:46 +03:00
self.text = text
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
# configure grid system (1x1)
2022-03-01 21:10:18 +03:00
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
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")
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,
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),
**kwargs)
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))
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()
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))
self.draw()
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)
def configure(self, require_redraw=False, **kwargs):
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)
if "text" in kwargs:
self.text = kwargs["text"]
self.text_label.configure(text=self.text)
del kwargs["text"]
if "text_font" in kwargs:
self.text_font = kwargs.pop("text_font")
self.text_label.configure(font=self.apply_font_scaling(self.text_font))
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"]
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"]
if "bg_color" in kwargs:
super().configure(bg_color=kwargs.pop("bg_color"), require_redraw=require_redraw)
else:
super().configure(require_redraw=require_redraw)
self.text_label.configure(**kwargs) # pass remaining kwargs to label
2021-03-04 20:27:46 +03:00
def set_text(self, text):
""" Will be removed in the next major release """
2021-09-13 15:27:29 +03:00
self.text = text
self.text_label.configure(text=self.text)