CustomTkinter/customtkinter/widgets/ctk_label.py

127 lines
5.7 KiB
Python
Raw Normal View History

2021-03-04 20:27:46 +03:00
import tkinter
from .ctk_canvas import CTkCanvas
2022-04-21 19:34:58 +03:00
from ..theme_manager import CTkThemeManager
from ..ctk_settings import CTkSettings
from ..ctk_draw_engine import CTkDrawEngine
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",
2021-03-04 20:27:46 +03:00
width=120,
height=25,
text="CTkLabel",
2022-02-23 03:47:34 +03:00
text_font="default_theme",
2021-03-04 20:27:46 +03:00
**kwargs):
# 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["master"])
del kwargs["master"]
else:
super().__init__(*args, bg_color=bg_color, width=width, height=height)
# color
self.fg_color = CTkThemeManager.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 = CTkThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
2021-03-04 20:27:46 +03:00
# shape
self.corner_radius = CTkThemeManager.theme["shape"]["label_corner_radius"] if corner_radius == "default_theme" else corner_radius
# text
2021-03-04 20:27:46 +03:00
self.text = text
self.text_font = (CTkThemeManager.theme["text"]["font"], CTkThemeManager.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-02 00:29:14 +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 = CTkDrawEngine(self.canvas, CTkSettings.preferred_drawing_method)
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,
text=self.text,
2022-04-21 19:34:58 +03:00
font=self.apply_font_scaling(self.text_font),
**kwargs)
2022-05-02 00:29:14 +03:00
self.text_label.grid(row=0, column=0, padx=self.apply_widget_scaling(self.corner_radius))
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)
self.canvas.configure(width=self.apply_widget_scaling(self.desired_width), height=self.apply_widget_scaling(self.desired_height))
self.text_label.configure(font=self.apply_font_scaling(self.text_font))
self.text_label.grid(row=0, column=0, padx=self.apply_widget_scaling(self.corner_radius))
def draw(self, no_color_updates=False):
2022-05-02 00:29:14 +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),
2022-04-21 19:34:58 +03:00
0)
2021-03-04 20:27:46 +03:00
self.canvas.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
2021-03-04 20:27:46 +03:00
if CTkThemeManager.single_color(self.fg_color, self.appearance_mode) is not None:
self.canvas.itemconfig("inner_parts",
fill=CTkThemeManager.single_color(self.fg_color, self.appearance_mode),
outline=CTkThemeManager.single_color(self.fg_color, self.appearance_mode))
2021-03-04 20:27:46 +03:00
self.text_label.configure(fg=CTkThemeManager.single_color(self.text_color, self.appearance_mode),
bg=CTkThemeManager.single_color(self.fg_color, self.appearance_mode))
else:
self.canvas.itemconfig("inner_parts",
fill=CTkThemeManager.single_color(self.bg_color, self.appearance_mode),
outline=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
self.text_label.configure(fg=CTkThemeManager.single_color(self.text_color, self.appearance_mode),
bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
2021-03-04 20:27:46 +03:00
def configure(self, *args, **kwargs):
require_redraw = False # some attribute changes require a call of self.draw() at the end
if "text" in kwargs:
self.set_text(kwargs["text"])
del kwargs["text"]
if "fg_color" in kwargs:
self.fg_color = kwargs["fg_color"]
require_redraw = True
del kwargs["fg_color"]
if "bg_color" in kwargs:
if kwargs["bg_color"] is None:
self.bg_color = self.detect_color_of_master()
else:
self.bg_color = kwargs["bg_color"]
require_redraw = True
del kwargs["bg_color"]
if "text_color" in kwargs:
self.text_color = kwargs["text_color"]
require_redraw = True
del kwargs["text_color"]
self.text_label.configure(*args, **kwargs)
if require_redraw:
self.draw()
2021-03-04 20:27:46 +03:00
def set_text(self, text):
2021-09-13 15:27:29 +03:00
self.text = text
self.text_label.configure(text=self.text, width=len(self.text))