From acaeceb96d5f9df9b678b4f9b0c1d5778aceea43 Mon Sep 17 00:00:00 2001 From: Tom Schimansky Date: Tue, 12 Jul 2022 21:52:52 +0200 Subject: [PATCH] added methods to CTkTextbox --- customtkinter/widgets/ctk_button.py | 4 +- customtkinter/widgets/ctk_label.py | 5 ++ customtkinter/widgets/ctk_textbox.py | 48 +++++++++++-------- examples/simple_example.py | 1 + .../complex_example_new.py | 10 ++-- 5 files changed, 44 insertions(+), 24 deletions(-) diff --git a/customtkinter/widgets/ctk_button.py b/customtkinter/widgets/ctk_button.py index e8f8abd..5e6867e 100644 --- a/customtkinter/widgets/ctk_button.py +++ b/customtkinter/widgets/ctk_button.py @@ -1,6 +1,6 @@ import tkinter import sys -from typing import Union, Tuple, Callable, Literal +from typing import Union, Tuple, Callable from .ctk_canvas import CTkCanvas from ..theme_manager import ThemeManager @@ -250,7 +250,7 @@ class CTkButton(CTkBaseClass): if "image" in kwargs: self.image = kwargs.pop("image") - self.set_image(self.image) + require_redraw = True if "compound" in kwargs: self.compound = kwargs.pop("compound") diff --git a/customtkinter/widgets/ctk_label.py b/customtkinter/widgets/ctk_label.py index ec3cfbc..033841d 100644 --- a/customtkinter/widgets/ctk_label.py +++ b/customtkinter/widgets/ctk_label.py @@ -1,3 +1,4 @@ +import sys import tkinter from .ctk_canvas import CTkCanvas @@ -106,6 +107,10 @@ class CTkLabel(CTkBaseClass): self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode)) + 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") diff --git a/customtkinter/widgets/ctk_textbox.py b/customtkinter/widgets/ctk_textbox.py index bd34842..9b40332 100644 --- a/customtkinter/widgets/ctk_textbox.py +++ b/customtkinter/widgets/ctk_textbox.py @@ -28,12 +28,13 @@ class CTkTextbox(CTkBaseClass): # color self.fg_color = ThemeManager.theme["color"]["entry"] if fg_color == "default_theme" else fg_color self.border_color = ThemeManager.theme["color"]["frame_border"] if border_color == "default_theme" else border_color + self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color # shape self.corner_radius = ThemeManager.theme["shape"]["frame_corner_radius"] if corner_radius == "default_theme" else corner_radius self.border_width = ThemeManager.theme["shape"]["frame_border_width"] if border_width == "default_theme" else border_width - self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color + # text self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font # configure 1x1 grid @@ -56,28 +57,18 @@ class CTkTextbox(CTkBaseClass): height=0, font=self.text_font, highlightthickness=0, + insertbackground=ThemeManager.single_color(("black", "white"), self._appearance_mode), bg=ThemeManager.single_color(self.fg_color, self._appearance_mode), **kwargs) self.textbox.grid(row=0, column=0, padx=self.corner_radius, pady=self.corner_radius, rowspan=1, columnspan=1, sticky="nsew") self.bind('', self.update_dimensions_event) - self.draw() - def winfo_children(self): - """ winfo_children of CTkFrame without self.canvas widget, - because it's not a child but part of the CTkFrame itself """ - - child_widgets = super().winfo_children() - try: - child_widgets.remove(self.canvas) - return child_widgets - except ValueError: - return child_widgets - def set_scaling(self, *args, **kwargs): super().set_scaling(*args, **kwargs) + self.textbox.configure(font=self.apply_font_scaling(self.text_font)) self.canvas.configure(width=self.apply_widget_scaling(self._desired_width), height=self.apply_widget_scaling(self._desired_height)) self.draw() @@ -110,17 +101,36 @@ class CTkTextbox(CTkBaseClass): outline=ThemeManager.single_color(self.border_color, self._appearance_mode)) self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode)) + self.textbox.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode), + bg=ThemeManager.single_color(self.fg_color, self._appearance_mode), + insertbackground=ThemeManager.single_color(("black", "white"), self._appearance_mode)) + self.canvas.tag_lower("inner_parts") self.canvas.tag_lower("border_parts") - def yview(self, args, kwargs): - self.textbox.yview(args, kwargs) + def yview(self, *args): + return self.textbox.yview(*args) - def xview(self, args, kwargs): - self.textbox.xview(args, kwargs) + def xview(self, *args): + return self.textbox.xview(*args) - def insert(self, args, kwargs): - self.textbox.insert(args, kwargs) + def insert(self, *args, **kwargs): + return self.textbox.insert(*args, **kwargs) + + def focus(self): + return self.textbox.focus() + + def tag_add(self, *args, **kwargs): + return self.textbox.tag_add(*args, **kwargs) + + def tag_config(self, *args, **kwargs): + return self.textbox.tag_config(*args, **kwargs) + + def tag_configure(self, *args, **kwargs): + return self.textbox.tag_configure(*args, **kwargs) + + def tag_remove(self, *args, **kwargs): + return self.textbox.tag_remove(*args, **kwargs) def configure(self, require_redraw=False, **kwargs): if "fg_color" in kwargs: diff --git a/examples/simple_example.py b/examples/simple_example.py index 370b4f4..31d916e 100644 --- a/examples/simple_example.py +++ b/examples/simple_example.py @@ -28,6 +28,7 @@ progressbar_1.pack(pady=12, padx=10) button_1 = customtkinter.CTkButton(master=frame_1, command=button_callback) button_1.pack(pady=12, padx=10) +button_1.configure(state='disabled') slider_1 = customtkinter.CTkSlider(master=frame_1, command=slider_callback, from_=0, to=1) slider_1.pack(pady=12, padx=10) diff --git a/test/manual_integration_tests/complex_example_new.py b/test/manual_integration_tests/complex_example_new.py index adff63c..20064b3 100644 --- a/test/manual_integration_tests/complex_example_new.py +++ b/test/manual_integration_tests/complex_example_new.py @@ -39,7 +39,7 @@ class App(customtkinter.CTk): self.appearance_mode_optionemenu.grid(row=6, column=0, padx=20, pady=(10, 10)) self.scaling_label = customtkinter.CTkLabel(self.sidebar_frame, text="Widget Scaling:", anchor="w") self.scaling_label.grid(row=7, column=0, padx=20, pady=(10, 0)) - self.scaling_optionemenu = customtkinter.CTkOptionMenu(self.sidebar_frame, values=["75%", "100%", "150%"], + self.scaling_optionemenu = customtkinter.CTkOptionMenu(self.sidebar_frame, values=["90%", "100%", "110%"], command=self.change_scaling) self.scaling_optionemenu.grid(row=8, column=0, padx=20, pady=(10, 20)) @@ -50,8 +50,8 @@ class App(customtkinter.CTk): self.main_button_1 = customtkinter.CTkButton(self, fg_color=None, border_width=2) self.main_button_1.grid(row=3, column=3, padx=(10, 20), pady=(10, 20), sticky="nsew") - self.text_frame = customtkinter.CTkFrame(self) - self.text_frame.grid(row=0, column=1, padx=(20, 10), pady=(20, 10), sticky="nsew") + self.textbox = customtkinter.CTkTextbox(self) + self.textbox.grid(row=0, column=1, padx=(20, 10), pady=(20, 10), sticky="nsew") # create radiobutton frame self.radiobutton_frame = customtkinter.CTkFrame(self) @@ -119,6 +119,10 @@ class App(customtkinter.CTk): self.scaling_optionemenu.set("100%") self.optionmenu_1.set("CTkOptionmenu") self.combobox_1.set("CTkComboBox") + self.textbox.insert("1.0", + "CTkTextbox\n\nLorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.", ) + #self.textbox.tag_add("headline", "1.0", "1.end") + #self.textbox.tag_config("headline", foreground="red") def open_input_dialog(self): dialog = customtkinter.CTkInputDialog(master=None, text="Type in a number:", title="CTkInputDialog")