diff --git a/customtkinter/__init__.py b/customtkinter/__init__.py index 0fd9484..62253bd 100644 --- a/customtkinter/__init__.py +++ b/customtkinter/__init__.py @@ -62,6 +62,7 @@ from .widgets.ctk_switch import CTkSwitch from .widgets.ctk_optionmenu import CTkOptionMenu from .widgets.ctk_combobox import CTkComboBox from .widgets.ctk_scrollbar import CTkScrollbar +from .widgets.ctk_textbox import CTkTextbox # import windows from .windows.ctk_tk import CTk diff --git a/customtkinter/appearance_mode_tracker.py b/customtkinter/appearance_mode_tracker.py index f805f31..5301e31 100644 --- a/customtkinter/appearance_mode_tracker.py +++ b/customtkinter/appearance_mode_tracker.py @@ -50,7 +50,10 @@ class AppearanceModeTracker: @classmethod def remove(cls, callback: Callable): - cls.callback_list.remove(callback) + try: + cls.callback_list.remove(callback) + except ValueError: + return @staticmethod def detect_appearance_mode() -> int: diff --git a/customtkinter/widgets/ctk_combobox.py b/customtkinter/widgets/ctk_combobox.py index fbe803f..7266a19 100644 --- a/customtkinter/widgets/ctk_combobox.py +++ b/customtkinter/widgets/ctk_combobox.py @@ -120,6 +120,13 @@ class CTkComboBox(CTkBaseClass): def set_scaling(self, *args, **kwargs): super().set_scaling(*args, **kwargs) + # change entry font size and grid padding + left_section_width = self._current_width - self._current_height + self.entry.configure(font=self.apply_font_scaling(self.text_font)) + self.entry.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="ew", + padx=(max(self.apply_widget_scaling(self.corner_radius), self.apply_widget_scaling(3)), + max(self.apply_widget_scaling(self._current_width - left_section_width + 3), self.apply_widget_scaling(3)))) + self.canvas.configure(width=self.apply_widget_scaling(self._desired_width), height=self.apply_widget_scaling(self._desired_height)) self.draw() diff --git a/customtkinter/widgets/ctk_optionmenu.py b/customtkinter/widgets/ctk_optionmenu.py index a10b3fd..d6df6fb 100644 --- a/customtkinter/widgets/ctk_optionmenu.py +++ b/customtkinter/widgets/ctk_optionmenu.py @@ -126,9 +126,12 @@ class CTkOptionMenu(CTkBaseClass): def set_scaling(self, *args, **kwargs): super().set_scaling(*args, **kwargs) - if self.text_label is not None: - self.text_label.destroy() - self.text_label = None + # change label text size and grid padding + left_section_width = self._current_width - self._current_height + self.text_label.configure(font=self.apply_font_scaling(self.text_font)) + self.text_label.grid(row=0, column=0, sticky="w", + padx=(max(self.apply_widget_scaling(self.corner_radius), self.apply_widget_scaling(3)), + max(self.apply_widget_scaling(self._current_width - left_section_width + 3), self.apply_widget_scaling(3)))) self.canvas.configure(width=self.apply_widget_scaling(self._desired_width), height=self.apply_widget_scaling(self._desired_height)) @@ -153,9 +156,6 @@ class CTkOptionMenu(CTkBaseClass): self.apply_widget_scaling(self._current_height / 2), self.apply_widget_scaling(self._current_height / 3)) - if self.current_value is not None: - self.text_label.configure(text=self.current_value) - if no_color_updates is False or requires_recoloring or requires_recoloring_2: self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode)) @@ -287,10 +287,7 @@ class CTkOptionMenu(CTkBaseClass): def set(self, value: str, from_variable_callback: bool = False): self.current_value = value - if self.text_label is not None: - self.text_label.configure(text=self.current_value) - else: - self.draw() + self.text_label.configure(text=self.current_value) if self.variable is not None and not from_variable_callback: self.variable_callback_blocked = True diff --git a/customtkinter/widgets/ctk_textbox.py b/customtkinter/widgets/ctk_textbox.py new file mode 100644 index 0000000..96e3410 --- /dev/null +++ b/customtkinter/widgets/ctk_textbox.py @@ -0,0 +1,159 @@ +import tkinter + +from .ctk_canvas import CTkCanvas +from ..theme_manager import ThemeManager +from ..draw_engine import DrawEngine +from .widget_base_class import CTkBaseClass + + +class CTkTextbox(CTkBaseClass): + def __init__(self, *args, + bg_color=None, + fg_color="default_theme", + border_color="default_theme", + border_width="default_theme", + corner_radius="default_theme", + text_font="default_theme", + text_color="default_theme", + width=200, + height=200, + **kwargs): + + # transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass + super().__init__(*args, bg_color=bg_color, width=width, height=height, **kwargs) + + # 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 + + # 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 + self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font + + # configure 1x1 grid + self.grid_rowconfigure(0, weight=1) + self.grid_columnconfigure(0, weight=1) + + self.canvas = CTkCanvas(master=self, + highlightthickness=0, + width=self.apply_widget_scaling(self._current_width), + height=self.apply_widget_scaling(self._current_height)) + self.canvas.grid(row=0, column=0, padx=0, pady=0, rowspan=1, columnspan=1, sticky="nsew") + self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode)) + self.draw_engine = DrawEngine(self.canvas) + + self.textbox = tkinter.Text(self, + fg=ThemeManager.single_color(self.text_color, self._appearance_mode), + width=0, + height=0, + font=self.text_font, + highlightthickness=0, + 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.canvas.configure(width=self.apply_widget_scaling(self._desired_width), height=self.apply_widget_scaling(self._desired_height)) + self.draw() + + def set_dimensions(self, width=None, height=None): + super().set_dimensions(width, height) + + 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): + + 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), + self.apply_widget_scaling(self.border_width)) + + if no_color_updates is False or requires_recoloring: + if self.fg_color is None: + self.canvas.itemconfig("inner_parts", + fill=ThemeManager.single_color(self.bg_color, self._appearance_mode), + outline=ThemeManager.single_color(self.bg_color, self._appearance_mode)) + else: + self.canvas.itemconfig("inner_parts", + fill=ThemeManager.single_color(self.fg_color, self._appearance_mode), + outline=ThemeManager.single_color(self.fg_color, self._appearance_mode)) + + self.canvas.itemconfig("border_parts", + fill=ThemeManager.single_color(self.border_color, self._appearance_mode), + outline=ThemeManager.single_color(self.border_color, self._appearance_mode)) + self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode)) + + self.canvas.tag_lower("inner_parts") + self.canvas.tag_lower("border_parts") + + def configure(self, *args, **kwargs): + require_redraw = False # some attribute changes require a call of self.draw() at the end + + if "fg_color" in kwargs: + self.fg_color = kwargs["fg_color"] + require_redraw = True + del kwargs["fg_color"] + + # check if CTk widgets are children of the frame and change their bg_color to new frame fg_color + for child in self.winfo_children(): + if isinstance(child, CTkBaseClass): + child.configure(bg_color=self.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 "border_color" in kwargs: + self.border_color = kwargs["border_color"] + require_redraw = True + del kwargs["border_color"] + + if "corner_radius" in kwargs: + self.corner_radius = kwargs["corner_radius"] + require_redraw = True + del kwargs["corner_radius"] + + if "border_width" in kwargs: + self.border_width = kwargs["border_width"] + require_redraw = True + del kwargs["border_width"] + + 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"] + + self.textbox.configure(*args, **kwargs) + + if require_redraw: + self.draw() diff --git a/customtkinter/widgets/widget_base_class.py b/customtkinter/widgets/widget_base_class.py index 9988ce0..29a5edb 100644 --- a/customtkinter/widgets/widget_base_class.py +++ b/customtkinter/widgets/widget_base_class.py @@ -138,7 +138,7 @@ class CTkBaseClass(tkinter.Frame): self.draw() def update_dimensions_event(self, event): - # only redraw if dimensions changed (for performance) + # only redraw if dimensions changed (for performance), independent of scaling if round(self._current_width) != round(event.width / self._widget_scaling) or round(self._current_height) != round(event.height / self._widget_scaling): self._current_width = (event.width / self._widget_scaling) # adjust current size according to new size given by event self._current_height = (event.height / self._widget_scaling) # _current_width and _current_height are independent of the scale diff --git a/test/manual_integration_tests/test_optionmenu_combobox.py b/test/manual_integration_tests/test_optionmenu_combobox.py index 3c5d698..31bf6da 100644 --- a/test/manual_integration_tests/test_optionmenu_combobox.py +++ b/test/manual_integration_tests/test_optionmenu_combobox.py @@ -4,7 +4,7 @@ import customtkinter app = customtkinter.CTk() app.title('Test OptionMenu ComboBox.py') -app.geometry('400x300') +app.geometry('400x500') def select_callback(choice): @@ -33,4 +33,12 @@ combobox_tk.pack(pady=10, padx=10) combobox_1 = customtkinter.CTkComboBox(app, variable=None, values=countries, command=select_callback, width=300) combobox_1.pack(pady=20, padx=10) +def set_new_scaling(scaling): + customtkinter.set_spacing_scaling(scaling) + customtkinter.set_window_scaling(scaling) + customtkinter.set_widget_scaling(scaling) + +scaling_slider = customtkinter.CTkSlider(app, command=set_new_scaling, from_=0, to=2) +scaling_slider.pack(pady=20, padx=10) + app.mainloop() diff --git a/test/manual_integration_tests/test_textbox.py b/test/manual_integration_tests/test_textbox.py new file mode 100644 index 0000000..f48f6dc --- /dev/null +++ b/test/manual_integration_tests/test_textbox.py @@ -0,0 +1,11 @@ +import customtkinter + +app = customtkinter.CTk() +app.grid_rowconfigure(0, weight=1) +app.grid_columnconfigure(0, weight=1) + +textbox = customtkinter.CTkTextbox(app) +textbox.grid(row=0, column=0, padx=20, pady=20, sticky="nsew") + + +app.mainloop()