From a2fcb5dee1766953272cda8a27637cc87c6f9279 Mon Sep 17 00:00:00 2001 From: Tom Schimansky Date: Tue, 5 Jul 2022 14:39:12 +0200 Subject: [PATCH] fixed variables for CTkCheckbox, CTkSwitch, CTkRadiobutton --- customtkinter/widgets/ctk_button.py | 70 ++++++------ customtkinter/widgets/ctk_checkbox.py | 101 ++++++------------ customtkinter/widgets/ctk_combobox.py | 54 ++++------ customtkinter/widgets/ctk_frame.py | 25 ++--- customtkinter/widgets/ctk_optionmenu.py | 8 +- customtkinter/widgets/ctk_radiobutton.py | 27 ++--- customtkinter/widgets/ctk_slider.py | 8 +- customtkinter/widgets/ctk_switch.py | 31 +++--- customtkinter/widgets/dropdown_menu.py | 17 ++- .../test_variables.py | 11 +- 10 files changed, 137 insertions(+), 215 deletions(-) diff --git a/customtkinter/widgets/ctk_button.py b/customtkinter/widgets/ctk_button.py index ccfdb50..ce9abce 100644 --- a/customtkinter/widgets/ctk_button.py +++ b/customtkinter/widgets/ctk_button.py @@ -57,7 +57,7 @@ class CTkButton(CTkBaseClass): self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font # callback and hover functionality - self.function = command + self.command = command self.textvariable = textvariable self.state = state self.hover = hover @@ -241,69 +241,59 @@ class CTkButton(CTkBaseClass): 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"] + self.text = kwargs.pop("text") + require_redraw = True # new text will be set in .draw() if "state" in kwargs: - self.state = kwargs["state"] + self.state = kwargs.pop("state") self.set_cursor() require_redraw = True - del kwargs["state"] if "image" in kwargs: - self.set_image(kwargs["image"]) - del kwargs["image"] + self.image = kwargs.pop("image") + self.set_image(self.image) if "compound" in kwargs: - self.compound = kwargs["compound"] + self.compound = kwargs.pop("compound") require_redraw = True - del kwargs["compound"] if "fg_color" in kwargs: - self.fg_color = kwargs["fg_color"] + self.fg_color = kwargs.pop("fg_color") require_redraw = True - del kwargs["fg_color"] if "border_color" in kwargs: - self.border_color = kwargs["border_color"] + self.border_color = kwargs.pop("border_color") require_redraw = True - del kwargs["border_color"] if "bg_color" in kwargs: - if kwargs["bg_color"] is None: + new_bg_color = kwargs.pop("bg_color") + if new_bg_color is None: self.bg_color = self.detect_color_of_master() else: - self.bg_color = kwargs["bg_color"] + self.bg_color = new_bg_color require_redraw = True - del kwargs["bg_color"] if "hover_color" in kwargs: - self.hover_color = kwargs["hover_color"] + self.hover_color = kwargs.pop("hover_color") require_redraw = True - del kwargs["hover_color"] if "text_color" in kwargs: - self.text_color = kwargs["text_color"] + self.text_color = kwargs.pop("text_color") require_redraw = True - del kwargs["text_color"] if "command" in kwargs: - self.function = kwargs["command"] - del kwargs["command"] + self.command = kwargs.pop("command") if "textvariable" in kwargs: - self.textvariable = kwargs["textvariable"] + self.textvariable = kwargs.pop("textvariable") if self.text_label is not None: self.text_label.configure(textvariable=self.textvariable) - del kwargs["textvariable"] if "width" in kwargs: - self.set_dimensions(width=kwargs["width"]) - del kwargs["width"] + self.set_dimensions(width=kwargs.pop("width")) if "height" in kwargs: - self.set_dimensions(height=kwargs["height"]) - del kwargs["height"] + self.set_dimensions(height=kwargs.pop("height")) super().configure(*args, **kwargs) @@ -313,24 +303,24 @@ class CTkButton(CTkBaseClass): def set_cursor(self): if Settings.cursor_manipulation_enabled: if self.state == tkinter.DISABLED: - if sys.platform == "darwin" and self.function is not None and Settings.cursor_manipulation_enabled: + if sys.platform == "darwin" and self.command is not None and Settings.cursor_manipulation_enabled: self.configure(cursor="arrow") - elif sys.platform.startswith("win") and self.function is not None and Settings.cursor_manipulation_enabled: + elif sys.platform.startswith("win") and self.command is not None and Settings.cursor_manipulation_enabled: self.configure(cursor="arrow") elif self.state == tkinter.NORMAL: - if sys.platform == "darwin" and self.function is not None and Settings.cursor_manipulation_enabled: + if sys.platform == "darwin" and self.command is not None and Settings.cursor_manipulation_enabled: self.configure(cursor="pointinghand") - elif sys.platform.startswith("win") and self.function is not None and Settings.cursor_manipulation_enabled: + elif sys.platform.startswith("win") and self.command is not None and Settings.cursor_manipulation_enabled: self.configure(cursor="hand2") - def set_text(self, text): - self.text = text - self.draw() - def set_image(self, image): - self.image = image - self.draw() + """ will be removed in next major """ + self.configure(image=image) + + def set_text(self, text): + """ will be removed in next major """ + self.configure(text=text) def on_enter(self, event=0): if self.hover is True and self.state == tkinter.NORMAL: @@ -379,7 +369,7 @@ class CTkButton(CTkBaseClass): self.on_enter() def clicked(self, event=0): - if self.function is not None: + if self.command is not None: if self.state is not tkinter.DISABLED: # click animation: change color with .on_leave() and back to normal after 100ms with click_animation() @@ -387,4 +377,4 @@ class CTkButton(CTkBaseClass): self.click_animation_running = True self.after(100, self.click_animation) - self.function() + self.command() diff --git a/customtkinter/widgets/ctk_checkbox.py b/customtkinter/widgets/ctk_checkbox.py index 1549c17..345da6c 100644 --- a/customtkinter/widgets/ctk_checkbox.py +++ b/customtkinter/widgets/ctk_checkbox.py @@ -49,24 +49,18 @@ class CTkCheckBox(CTkBaseClass): self.border_width = ThemeManager.theme["shape"]["checkbox_border_width"] if border_width == "default_theme" else border_width # text - if textvariable is None: - self.text = text - else: - self.text = textvariable.get() - + self.text = text self.text_label: Union[tkinter.Label, None] = None self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color self.text_color_disabled = ThemeManager.theme["color"]["text_disabled"] if text_color_disabled == "default_theme" else text_color_disabled self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font # callback and hover functionality - self.function = command + self.command = command self.state = state self.hover = hover - if variable == None: - self.check_state = False - else: - self.check_state = (variable.get() == onvalue) + self.check_state = False + self.onvalue = onvalue self.offvalue = offvalue self.variable: tkinter.Variable = variable @@ -110,13 +104,11 @@ class CTkCheckBox(CTkBaseClass): self.text_label.bind("", self.on_leave) self.text_label.bind("", self.toggle) - # set select state according to variable - if self.variable is not None: + # register variable callback and set state according to variable + if self.variable is not None and self.variable != "": self.variable_callback_name = self.variable.trace_add("write", self.variable_callback) - - if self.textvariable is not None: - self.textvariable_callback_name = self.textvariable.trace_add("write", self.textvariable_callback) - + self.check_state = True if variable.get() == self.onvalue else False + self.draw() # initial draw self.set_cursor() @@ -176,89 +168,61 @@ class CTkCheckBox(CTkBaseClass): self.text_label.configure(fg=(ThemeManager.single_color(self.text_color_disabled, self._appearance_mode))) else: self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode)) + self.text_label.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode)) def configure(self, *args, **kwargs): require_redraw = False # some attribute changes require a call of self.draw() if "text" in kwargs: - self.text = kwargs["text"] + self.text = kwargs.pop("text") self.text_label.configure(text=self.text) - del kwargs["text"] if "state" in kwargs: - self.state = kwargs["state"] + self.state = kwargs.pop("state") self.set_cursor() require_redraw = True - del kwargs["state"] if "fg_color" in kwargs: - self.fg_color = kwargs["fg_color"] + self.fg_color = kwargs.pop("fg_color") require_redraw = True - del kwargs["fg_color"] if "bg_color" in kwargs: - if kwargs["bg_color"] is None: + new_bg_color = kwargs.pop("bg_color") + if new_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"] + self.bg_color = new_bg_color if "hover_color" in kwargs: - self.hover_color = kwargs["hover_color"] + self.hover_color = kwargs.pop("hover_color") require_redraw = True - del kwargs["hover_color"] if "text_color" in kwargs: - self.text_color = kwargs["text_color"] + self.text_color = kwargs.pop("text_color") require_redraw = True - del kwargs["text_color"] if "border_color" in kwargs: - self.border_color = kwargs["border_color"] + self.border_color = kwargs.pop("border_color") require_redraw = True - del kwargs["border_color"] if "command" in kwargs: - self.function = kwargs["command"] - del kwargs["command"] + self.command = kwargs.pop("command") if "textvariable" in kwargs: - self.textvariable = kwargs["textvariable"] + self.textvariable = kwargs.pop("textvariable") self.text_label.configure(textvariable=self.textvariable) - del kwargs["textvariable"] if "variable" in kwargs: - if self.variable is not None: - self.variable.trace_remove("write", self.variable_callback_name) + if self.variable is not None and self.variable != "": + self.variable.trace_remove("write", self.variable_callback_name) # remove old variable callback - self.variable = kwargs["variable"] + self.variable = kwargs.pop("variable") if self.variable is not None and self.variable != "": self.variable_callback_name = self.variable.trace_add("write", self.variable_callback) - if self.variable.get() == self.onvalue: - self.select(from_variable_callback=True) - elif self.variable.get() == self.offvalue: - self.deselect(from_variable_callback=True) - else: - self.variable = None - - del kwargs["variable"] - - if "textvariable" in kwargs: - if self.textvariable is not None: - self.textvariable.trace_remove("write", self.textvariable_callback_name) - - self.textvariable = kwargs["textvariable"] - - if self.textvariable is not None and self.textvariable != "": - self.textvariable_callback_name = self.textvariable.trace_add("write", self.textvariable_callback) - self.set_text(self.textvariable.get()) - else: - self.textvariable = None - - del kwargs["textvariable"] + self.check_state = True if self.variable.get() == self.onvalue else False + require_redraw = True super().configure(*args, **kwargs) @@ -325,9 +289,6 @@ class CTkCheckBox(CTkBaseClass): elif self.variable.get() == self.offvalue: self.deselect(from_variable_callback=True) - def textvariable_callback(self, var_name, index, mode): - self.set_text(self.textvariable.get()) - def toggle(self, event=0): if self.state == tkinter.NORMAL: if self.check_state is True: @@ -342,8 +303,8 @@ class CTkCheckBox(CTkBaseClass): self.variable.set(self.onvalue if self.check_state is True else self.offvalue) self.variable_callback_blocked = False - if self.function is not None: - self.function() + if self.command is not None: + self.command() def select(self, from_variable_callback=False): self.check_state = True @@ -354,8 +315,8 @@ class CTkCheckBox(CTkBaseClass): self.variable.set(self.onvalue) self.variable_callback_blocked = False - if self.function is not None: - self.function() + if self.command is not None: + self.command() def deselect(self, from_variable_callback=False): self.check_state = False @@ -366,8 +327,8 @@ class CTkCheckBox(CTkBaseClass): self.variable.set(self.offvalue) self.variable_callback_blocked = False - if self.function is not None: - self.function() + if self.command is not None: + self.command() def get(self): return self.onvalue if self.check_state is True else self.offvalue diff --git a/customtkinter/widgets/ctk_combobox.py b/customtkinter/widgets/ctk_combobox.py index 7266a19..deb3a06 100644 --- a/customtkinter/widgets/ctk_combobox.py +++ b/customtkinter/widgets/ctk_combobox.py @@ -55,7 +55,7 @@ class CTkComboBox(CTkBaseClass): self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font # callback and hover functionality - self.function = command + self.command = command self.variable = variable self.state = state self.hover = hover @@ -187,76 +187,62 @@ class CTkComboBox(CTkBaseClass): require_redraw = False # some attribute changes require a call of self.draw() at the end if "state" in kwargs: - self.state = kwargs["state"] + self.state = kwargs.pop("state") self.entry.configure(state=self.state) require_redraw = True - del kwargs["state"] if "fg_color" in kwargs: - self.fg_color = kwargs["fg_color"] + self.fg_color = kwargs.pop("fg_color") require_redraw = True - del kwargs["fg_color"] if "bg_color" in kwargs: - if kwargs["bg_color"] is None: + new_bg_color = kwargs.pop("bg_color") + if new_bg_color is None: self.bg_color = self.detect_color_of_master() else: - self.bg_color = kwargs["bg_color"] + self.bg_color = new_bg_color require_redraw = True - del kwargs["bg_color"] if "button_color" in kwargs: - self.button_color = kwargs["button_color"] + self.button_color = kwargs.pop("button_color") require_redraw = True - del kwargs["button_color"] if "button_hover_color" in kwargs: - self.button_hover_color = kwargs["button_hover_color"] + self.button_hover_color = kwargs.pop("button_hover_color") require_redraw = True - del kwargs["button_hover_color"] if "text_color" in kwargs: - self.text_color = kwargs["text_color"] + self.text_color = kwargs.pop("text_color") require_redraw = True - del kwargs["text_color"] if "command" in kwargs: - self.function = kwargs["command"] - del kwargs["command"] + self.command = kwargs.pop("command") if "variable" in kwargs: - self.variable = kwargs["variable"] + self.variable = kwargs.pop("variable") self.entry.configure(textvariable=self.variable) - del kwargs["variable"] if "width" in kwargs: - self.set_dimensions(width=kwargs["width"]) - del kwargs["width"] + self.set_dimensions(width=kwargs.pop("width")) if "height" in kwargs: - self.set_dimensions(height=kwargs["height"]) - del kwargs["height"] + self.set_dimensions(height=kwargs.pop("height")) if "values" in kwargs: - self.values = kwargs["values"] - del kwargs["values"] + self.values = kwargs.pop("values") self.dropdown_menu.configure(values=self.values) if "dropdown_color" in kwargs: - self.dropdown_menu.configure(fg_color=kwargs["dropdown_color"]) - del kwargs["dropdown_color"] + self.dropdown_menu.configure(fg_color=kwargs.pop("dropdown_color")) if "dropdown_hover_color" in kwargs: - self.dropdown_menu.configure(hover_color=kwargs["dropdown_hover_color"]) - del kwargs["dropdown_hover_color"] + self.dropdown_menu.configure(hover_color=kwargs.pop("dropdown_hover_color")) if "dropdown_text_color" in kwargs: - self.dropdown_menu.configure(text_color=kwargs["dropdown_text_color"]) - del kwargs["dropdown_text_color"] + self.dropdown_menu.configure(text_color=kwargs.pop("dropdown_text_color")) if "dropdown_text_font" in kwargs: - self.dropdown_menu.configure(text_font=kwargs["dropdown_text_font"]) - del kwargs["dropdown_text_font"] + self.dropdown_menu.configure(text_font=kwargs.pop("dropdown_text_font")) super().configure(*args, **kwargs) @@ -300,8 +286,8 @@ class CTkComboBox(CTkBaseClass): self.entry.insert(0, self.current_value) if not from_variable_callback: - if self.function is not None: - self.function(self.current_value) + if self.command is not None: + self.command(self.current_value) def get(self) -> str: return self.entry.get() diff --git a/customtkinter/widgets/ctk_frame.py b/customtkinter/widgets/ctk_frame.py index b377451..2d7a28a 100644 --- a/customtkinter/widgets/ctk_frame.py +++ b/customtkinter/widgets/ctk_frame.py @@ -105,9 +105,8 @@ class CTkFrame(CTkBaseClass): 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"] + self.fg_color = kwargs.pop("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(): @@ -115,36 +114,30 @@ class CTkFrame(CTkBaseClass): child.configure(bg_color=self.fg_color) if "bg_color" in kwargs: - if kwargs["bg_color"] is None: + new_bg_color = kwargs.pop("bg_color") + if new_bg_color is None: self.bg_color = self.detect_color_of_master() else: - self.bg_color = kwargs["bg_color"] + self.bg_color = new_bg_color require_redraw = True - del kwargs["bg_color"] - if "border_color" in kwargs: - self.border_color = kwargs["border_color"] + self.border_color = kwargs.pop("border_color") require_redraw = True - del kwargs["border_color"] if "corner_radius" in kwargs: - self.corner_radius = kwargs["corner_radius"] + self.corner_radius = kwargs.pop("corner_radius") require_redraw = True - del kwargs["corner_radius"] if "border_width" in kwargs: - self.border_width = kwargs["border_width"] + self.border_width = kwargs.pop("border_width") require_redraw = True - del kwargs["border_width"] if "width" in kwargs: - self.set_dimensions(width=kwargs["width"]) - del kwargs["width"] + self.set_dimensions(width=kwargs.pop("width")) if "height" in kwargs: - self.set_dimensions(height=kwargs["height"]) - del kwargs["height"] + self.set_dimensions(height=kwargs.pop("height")) super().configure(*args, **kwargs) diff --git a/customtkinter/widgets/ctk_optionmenu.py b/customtkinter/widgets/ctk_optionmenu.py index 6c5cdee..03e3476 100644 --- a/customtkinter/widgets/ctk_optionmenu.py +++ b/customtkinter/widgets/ctk_optionmenu.py @@ -52,7 +52,7 @@ class CTkOptionMenu(CTkBaseClass): self.dropdown_text_font = dropdown_text_font # callback and hover functionality - self.function = command + self.command = command self.variable = variable self.variable_callback_blocked = False self.variable_callback_name = None @@ -221,7 +221,7 @@ class CTkOptionMenu(CTkBaseClass): require_redraw = True if "command" in kwargs: - self.function = kwargs.pop("command") + self.command = kwargs.pop("command") if "variable" in kwargs: if self.variable is not None: # remove old callback @@ -298,8 +298,8 @@ class CTkOptionMenu(CTkBaseClass): self.variable_callback_blocked = False if not from_variable_callback: - if self.function is not None: - self.function(self.current_value) + if self.command is not None: + self.command(self.current_value) def get(self) -> str: return self.current_value diff --git a/customtkinter/widgets/ctk_radiobutton.py b/customtkinter/widgets/ctk_radiobutton.py index 76c4b3d..711bf46 100644 --- a/customtkinter/widgets/ctk_radiobutton.py +++ b/customtkinter/widgets/ctk_radiobutton.py @@ -54,7 +54,7 @@ class CTkRadioButton(CTkBaseClass): self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font # callback and control variables - self.function = command + self.command = command self.state = state self.hover = hover self.check_state = False @@ -99,15 +99,12 @@ class CTkRadioButton(CTkBaseClass): self.text_label.bind("", self.on_leave) self.text_label.bind("", self.invoke) - self.draw() # initial draw - self.set_cursor() - if self.variable is not None: self.variable_callback_name = self.variable.trace_add("write", self.variable_callback) - if self.variable.get() == self.value: - self.select(from_variable_callback=True) - else: - self.deselect(from_variable_callback=True) + self.check_state = True if self.variable.get() == self.value else False + + self.draw() # initial draw + self.set_cursor() def set_scaling(self, *args, **kwargs): super().set_scaling(*args, **kwargs) @@ -195,7 +192,7 @@ class CTkRadioButton(CTkBaseClass): require_redraw = True if "command" in kwargs: - self.function = kwargs.pop("command") + self.command = kwargs.pop("command") if "textvariable" in kwargs: self.textvariable = kwargs.pop("textvariable") @@ -209,12 +206,8 @@ class CTkRadioButton(CTkBaseClass): if self.variable is not None and self.variable != "": self.variable_callback_name = self.variable.trace_add("write", self.variable_callback) - if self.variable.get() == self.value: - self.select(from_variable_callback=True) - else: - self.deselect(from_variable_callback=True) - else: - self.variable = None + self.check_state = True if self.variable.get() == self.value else False + require_redraw = True super().configure(*args, **kwargs) @@ -273,8 +266,8 @@ class CTkRadioButton(CTkBaseClass): self.check_state = True self.select() - if self.function is not None: - self.function() + if self.command is not None: + self.command() def select(self, from_variable_callback=False): self.check_state = True diff --git a/customtkinter/widgets/ctk_slider.py b/customtkinter/widgets/ctk_slider.py index 91e9786..82906f3 100644 --- a/customtkinter/widgets/ctk_slider.py +++ b/customtkinter/widgets/ctk_slider.py @@ -72,7 +72,7 @@ class CTkSlider(CTkBaseClass): self.corner_radius = self.button_corner_radius # callback and control variables - self.callback_function = command + self.command = command self.variable: tkinter.Variable = variable self.variable_callback_blocked = False self.variable_callback_name = None @@ -205,8 +205,8 @@ class CTkSlider(CTkBaseClass): self.variable.set(round(self.output_value) if isinstance(self.variable, tkinter.IntVar) else self.output_value) self.variable_callback_blocked = False - if self.callback_function is not None: - self.callback_function(self.output_value) + if self.command is not None: + self.command(self.output_value) def on_enter(self, event=0): if self.state == "normal": @@ -321,7 +321,7 @@ class CTkSlider(CTkBaseClass): del kwargs["number_of_steps"] if "command" in kwargs: - self.callback_function = kwargs["command"] + self.command = kwargs["command"] del kwargs["command"] if "variable" in kwargs: diff --git a/customtkinter/widgets/ctk_switch.py b/customtkinter/widgets/ctk_switch.py index 7a909be..2bcb407 100644 --- a/customtkinter/widgets/ctk_switch.py +++ b/customtkinter/widgets/ctk_switch.py @@ -63,7 +63,7 @@ class CTkSwitch(CTkBaseClass): self.offvalue = offvalue # callback and control variables - self.function = command + self.command = command self.variable: tkinter.Variable = variable self.variable_callback_blocked = False self.variable_callback_name = None @@ -107,12 +107,9 @@ class CTkSwitch(CTkBaseClass): self.draw() # initial draw self.set_cursor() - if self.variable is not None: + if self.variable is not None and self.variable != "": self.variable_callback_name = self.variable.trace_add("write", self.variable_callback) - if self.variable.get() == self.onvalue: - self.select(from_variable_callback=True) - elif self.variable.get() == self.offvalue: - self.deselect(from_variable_callback=True) + self.check_state = True if self.variable.get() == self.onvalue else False def set_scaling(self, *args, **kwargs): super().set_scaling(*args, **kwargs) @@ -212,8 +209,8 @@ class CTkSwitch(CTkBaseClass): self.draw(no_color_updates=True) - if self.function is not None: - self.function() + if self.command is not None: + self.command() if self.variable is not None: self.variable_callback_blocked = True @@ -231,8 +228,8 @@ class CTkSwitch(CTkBaseClass): self.variable.set(self.onvalue) self.variable_callback_blocked = False - if self.function is not None: - self.function() + if self.command is not None: + self.command() def deselect(self, from_variable_callback=False): if self.state is not tkinter.DISABLED or from_variable_callback: @@ -245,8 +242,8 @@ class CTkSwitch(CTkBaseClass): self.variable.set(self.offvalue) self.variable_callback_blocked = False - if self.function is not None: - self.function() + if self.command is not None: + self.command() def get(self): return self.onvalue if self.check_state is True else self.offvalue @@ -319,24 +316,22 @@ class CTkSwitch(CTkBaseClass): require_redraw = True if "command" in kwargs: - self.function = kwargs.pop("command") + self.command = kwargs.pop("command") if "textvariable" in kwargs: self.textvariable = kwargs.pop("textvariable") self.text_label.configure(textvariable=self.textvariable) if "variable" in kwargs: - if self.variable is not None: + if self.variable is not None and self.variable != "": self.variable.trace_remove("write", self.variable_callback_name) self.variable = kwargs.pop("variable") if self.variable is not None and self.variable != "": self.variable_callback_name = self.variable.trace_add("write", self.variable_callback) - if self.variable.get() == self.onvalue: - self.select(from_variable_callback=True) - elif self.variable.get() == self.offvalue: - self.deselect(from_variable_callback=True) + self.check_state = True if self.variable.get() == self.onvalue else False + require_redraw = True else: self.variable = None diff --git a/customtkinter/widgets/dropdown_menu.py b/customtkinter/widgets/dropdown_menu.py index 4c0fde6..a553c47 100644 --- a/customtkinter/widgets/dropdown_menu.py +++ b/customtkinter/widgets/dropdown_menu.py @@ -42,6 +42,8 @@ class DropdownMenu(tkinter.Menu): self.add_menu_commands() def configure_menu_for_platforms(self): + """ apply platform specific appearance attributes """ + if sys.platform == "darwin": self.configure(tearoff=False, font=self.apply_font_scaling(self.text_font)) @@ -98,29 +100,24 @@ class DropdownMenu(tkinter.Menu): def configure(self, **kwargs): if "values" in kwargs: - self.values = kwargs["values"] - del kwargs["values"] + self.values = kwargs.pop("values") self.delete(0, "end") # delete all old commands self.add_menu_commands() if "fg_color" in kwargs: - self.fg_color = kwargs["fg_color"] - del kwargs["fg_color"] + self.fg_color = kwargs.pop("fg_color") self.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode)) if "hover_color" in kwargs: - self.hover_color = kwargs["hover_color"] - del kwargs["hover_color"] + self.hover_color = kwargs.pop("hover_color") self.configure(activebackground=ThemeManager.single_color(self.hover_color, self._appearance_mode)) if "text_color" in kwargs: - self.text_color = kwargs["text_color"] - del kwargs["text_color"] + self.text_color = kwargs.pop("text_color") self.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode)) if "text_font" in kwargs: - self.text_font = kwargs["text_font"] - del kwargs["text_font"] + self.text_font = kwargs.pop("text_font") self.configure(font=self.apply_font_scaling(self.text_font)) super().configure(**kwargs) diff --git a/test/manual_integration_tests/test_variables.py b/test/manual_integration_tests/test_variables.py index 1e9d173..a8fb5df 100644 --- a/test/manual_integration_tests/test_variables.py +++ b/test/manual_integration_tests/test_variables.py @@ -8,12 +8,17 @@ app = customtkinter.CTk() # create CTk window like you do with the Tk window (y app.geometry("400x900") app.title("Tkinter Variable Test") +def checkbox_event(): + print("checkbox_event") + txt_var = tkinter.StringVar(value="") entry_1 = customtkinter.CTkEntry(app, width=200, textvariable=txt_var) entry_1.pack(pady=15) txt_var.set("new text test") if TEST_CONFIGURE: entry_1.configure(textvariable=txt_var) if TEST_REMOVING: entry_1.configure(textvariable="") +#entry_1.delete(0, "end") +#entry_1.insert(0, "sadsad") label_1 = customtkinter.CTkLabel(app, width=200, textvariable=txt_var) label_1.pack(pady=15) @@ -46,11 +51,13 @@ if TEST_CONFIGURE: progress_1.configure(variable=int_var) if TEST_REMOVING: progress_1.configure(variable="") check_var = tkinter.StringVar(value="on") -check_1 = customtkinter.CTkCheckBox(app, text="check 1", variable=check_var, onvalue="on", offvalue="off", textvariable=txt_var) +check_1 = customtkinter.CTkCheckBox(app, text="check 1", variable=check_var, onvalue="on", offvalue="off", textvariable=txt_var, + command=checkbox_event) check_1.pack(pady=15) if TEST_CONFIGURE: check_1.configure(variable=check_var) if TEST_REMOVING: check_1.configure(variable="") -print("check_1", check_1.get()) + +print("check 1 created") check_2 = customtkinter.CTkCheckBox(app, text="check 2", variable=check_var, onvalue="on", offvalue="off") check_2.pack(pady=15)