From 8c9183006cb19a8943539e1c04506e303e4476c6 Mon Sep 17 00:00:00 2001 From: Tom Schimansky Date: Tue, 16 Aug 2022 14:05:15 +0200 Subject: [PATCH] added text_font configuration for all CTk widgets #266 --- customtkinter/widgets/ctk_button.py | 5 ++ customtkinter/widgets/ctk_checkbox.py | 5 ++ customtkinter/widgets/ctk_combobox.py | 4 ++ customtkinter/widgets/ctk_entry.py | 4 ++ customtkinter/widgets/ctk_label.py | 4 ++ customtkinter/widgets/ctk_optionmenu.py | 7 +- customtkinter/widgets/ctk_radiobutton.py | 4 ++ customtkinter/widgets/ctk_switch.py | 4 ++ customtkinter/widgets/ctk_textbox.py | 7 ++ .../test_new_menu_design.py | 71 ------------------- 10 files changed, 43 insertions(+), 72 deletions(-) delete mode 100644 test/manual_integration_tests/test_new_menu_design.py diff --git a/customtkinter/widgets/ctk_button.py b/customtkinter/widgets/ctk_button.py index ceb64f6..09acdc8 100644 --- a/customtkinter/widgets/ctk_button.py +++ b/customtkinter/widgets/ctk_button.py @@ -243,6 +243,11 @@ class CTkButton(CTkBaseClass): else: self.text_label.configure(text=self.text) + if "text_font" in kwargs: + self.text_font = kwargs.pop("text_font") + if self.text_label is not None: + self.text_label.configure(font=self.apply_font_scaling(self.text_font)) + if "state" in kwargs: self.state = kwargs.pop("state") self.set_cursor() diff --git a/customtkinter/widgets/ctk_checkbox.py b/customtkinter/widgets/ctk_checkbox.py index 787f1e6..18f5883 100644 --- a/customtkinter/widgets/ctk_checkbox.py +++ b/customtkinter/widgets/ctk_checkbox.py @@ -177,6 +177,11 @@ class CTkCheckBox(CTkBaseClass): self.text = kwargs.pop("text") self.text_label.configure(text=self.text) + if "text_font" in kwargs: + self.text_font = kwargs.pop("text_font") + if self.text_label is not None: + self.text_label.configure(font=self.apply_font_scaling(self.text_font)) + if "state" in kwargs: self.state = kwargs.pop("state") self.set_cursor() diff --git a/customtkinter/widgets/ctk_combobox.py b/customtkinter/widgets/ctk_combobox.py index 35347d2..0dbc0d3 100644 --- a/customtkinter/widgets/ctk_combobox.py +++ b/customtkinter/widgets/ctk_combobox.py @@ -203,6 +203,10 @@ class CTkComboBox(CTkBaseClass): self.text_color = kwargs.pop("text_color") require_redraw = True + if "text_font" in kwargs: + self.text_font = kwargs.pop("text_font") + self.entry.configure(font=self.apply_font_scaling(self.text_font)) + if "command" in kwargs: self.command = kwargs.pop("command") diff --git a/customtkinter/widgets/ctk_entry.py b/customtkinter/widgets/ctk_entry.py index 1fb5d21..3dbe89f 100644 --- a/customtkinter/widgets/ctk_entry.py +++ b/customtkinter/widgets/ctk_entry.py @@ -185,6 +185,10 @@ class CTkEntry(CTkBaseClass): self.textvariable = kwargs.pop("textvariable") self.entry.configure(textvariable=self.textvariable) + if "text_font" in kwargs: + self.text_font = kwargs.pop("text_font") + self.entry.configure(font=self.apply_font_scaling(self.text_font)) + if "show" in kwargs: if self.placeholder_text_active: self.pre_placeholder_arguments["show"] = kwargs.pop("show") diff --git a/customtkinter/widgets/ctk_label.py b/customtkinter/widgets/ctk_label.py index c60699b..cac1154 100644 --- a/customtkinter/widgets/ctk_label.py +++ b/customtkinter/widgets/ctk_label.py @@ -123,6 +123,10 @@ class CTkLabel(CTkBaseClass): 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 diff --git a/customtkinter/widgets/ctk_optionmenu.py b/customtkinter/widgets/ctk_optionmenu.py index dd6f5f5..95e67e7 100644 --- a/customtkinter/widgets/ctk_optionmenu.py +++ b/customtkinter/widgets/ctk_optionmenu.py @@ -210,6 +210,10 @@ class CTkOptionMenu(CTkBaseClass): self.text_color = kwargs.pop("text_color") require_redraw = True + 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 "command" in kwargs: self.command = kwargs.pop("command") @@ -245,7 +249,8 @@ class CTkOptionMenu(CTkBaseClass): self.dropdown_menu.configure(text_color=kwargs.pop("dropdown_text_color")) if "dropdown_text_font" in kwargs: - self.dropdown_menu.configure(text_font=kwargs.pop("dropdown_text_font")) + self.dropdown_text_font = kwargs.pop("dropdown_text_font") + self.dropdown_menu.configure(text_font=self.dropdown_text_font) if "dynamic_resizing" in kwargs: self.dynamic_resizing = kwargs.pop("dynamic_resizing") diff --git a/customtkinter/widgets/ctk_radiobutton.py b/customtkinter/widgets/ctk_radiobutton.py index c782fed..6082af1 100644 --- a/customtkinter/widgets/ctk_radiobutton.py +++ b/customtkinter/widgets/ctk_radiobutton.py @@ -156,6 +156,10 @@ class CTkRadioButton(CTkBaseClass): self.text = kwargs.pop("text") self.text_label.configure(text=self.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 "state" in kwargs: self.state = kwargs.pop("state") self.set_cursor() diff --git a/customtkinter/widgets/ctk_switch.py b/customtkinter/widgets/ctk_switch.py index f1de210..e9dc152 100644 --- a/customtkinter/widgets/ctk_switch.py +++ b/customtkinter/widgets/ctk_switch.py @@ -272,6 +272,10 @@ class CTkSwitch(CTkBaseClass): self.text = kwargs.pop("text") self.text_label.configure(text=self.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 "state" in kwargs: self.state = kwargs.pop("state") self.set_cursor() diff --git a/customtkinter/widgets/ctk_textbox.py b/customtkinter/widgets/ctk_textbox.py index 6dfedb2..bad0d35 100644 --- a/customtkinter/widgets/ctk_textbox.py +++ b/customtkinter/widgets/ctk_textbox.py @@ -161,6 +161,13 @@ class CTkTextbox(CTkBaseClass): if "height" in kwargs: self.set_dimensions(height=kwargs.pop("height")) + if "text_font" in kwargs: + self.text_font = kwargs.pop("text_font") + self.textbox.configure(font=self.apply_font_scaling(self.text_font)) + + if "font" in kwargs: + raise ValueError("No attribute named font. Use text_font instead of font for CTk widgets") + if "bg_color" in kwargs: super().configure(bg_color=kwargs.pop("bg_color"), require_redraw=require_redraw) else: diff --git a/test/manual_integration_tests/test_new_menu_design.py b/test/manual_integration_tests/test_new_menu_design.py deleted file mode 100644 index 40d39a2..0000000 --- a/test/manual_integration_tests/test_new_menu_design.py +++ /dev/null @@ -1,71 +0,0 @@ -import customtkinter -import tkinter -import sys - - -class CTkMenu(tkinter.Toplevel): - def __init__(self, master, x, y, options): - super().__init__() - - self.overrideredirect(True) - self.geometry(f"120x{len(options) * (25 + 3) + 3}+{x}+{y}") - - if sys.platform.startswith("darwin"): - self.overrideredirect(False) - self.wm_attributes("-transparent", True) # turn off shadow - self.config(bg='systemTransparent') # transparent bg - self.frame = customtkinter.CTkFrame(self, border_width=0, width=120, corner_radius=6, border_color="gray4", fg_color="#333740") - elif sys.platform.startswith("win"): - self.configure(bg="#FFFFF1") - self.wm_attributes("-transparent", "#FFFFF1") - self.focus() - self.frame = customtkinter.CTkFrame(self, border_width=0, width=120, corner_radius=6, border_color="gray4", fg_color="#333740", - overwrite_preferred_drawing_method="circle_shapes") - else: - self.configure(bg="#FFFFF1") - self.wm_attributes("-transparent", "#FFFFF1") - self.frame = customtkinter.CTkFrame(self, border_width=0, width=120, corner_radius=6, border_color="gray4", fg_color="#333740", - overwrite_preferred_drawing_method="circle_shapes") - - self.frame.grid(row=0, column=0, sticky="nsew", rowspan=len(options) + 1, columnspan=1, ipadx=0, ipady=0) - - self.frame.grid_rowconfigure(len(options) + 1, minsize=3) - self.frame.grid_columnconfigure(0, weight=1) - self.grid_columnconfigure(0, weight=1) - - self.buttons = [] - for index, option in enumerate(options): - button = customtkinter.CTkButton(self.frame, text=option, height=25, width=108, fg_color="#333740", text_color="gray74", - hover_color="gray28", corner_radius=4, command=self.button_click) - button.text_label.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="w") - button.grid(row=index, column=0, padx=(3, 3), pady=(3, 0), columnspan=1, rowspan=1, sticky="ew") - self.buttons.append(button) - - self.bind("", self.focus_loss_event) - self.frame.canvas.bind("", self.focus_loss_event) - - def focus_loss_event(self, event): - print("focus loss") - self.destroy() - # self.update() - - def button_click(self): - print("button press") - self.destroy() - # self.update() - - -app = customtkinter.CTk() -app.geometry("600x500") - - -def open_menu(): - menu = CTkMenu(app, button.winfo_rootx(), button.winfo_rooty() + button.winfo_height() + 4, ["Option 1", "Option 2", "Point 3"]) - -button = customtkinter.CTkButton(command=open_menu, height=30, corner_radius=6) -button.pack(pady=20) - -button_2 = customtkinter.CTkButton(command=open_menu, height=30, corner_radius=6) -button_2.pack(pady=60) - -app.mainloop()