diff --git a/customtkinter/widgets/ctk_combobox.py b/customtkinter/widgets/ctk_combobox.py index 3af5186..eda3a64 100644 --- a/customtkinter/widgets/ctk_combobox.py +++ b/customtkinter/widgets/ctk_combobox.py @@ -63,14 +63,9 @@ class CTkComboBox(CTkBaseClass): else: self.values = values - if len(self.values) > 0: - self.current_value = self.values[0] - else: - self.current_value = "CTkComboBox" - self.dropdown_menu = DropdownMenu(master=self, values=self.values, - command=self.set, + command=self.dropdown_callback, fg_color=dropdown_color, hover_color=dropdown_hover_color, text_color=dropdown_text_color, @@ -98,8 +93,11 @@ class CTkComboBox(CTkBaseClass): 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.entry.delete(0, tkinter.END) - self.entry.insert(0, self.current_value) + # insert default value + if len(self.values) > 0: + self.entry.insert(0, self.values[0]) + else: + self.entry.insert(0, "CTkComboBox") self.draw() # initial draw @@ -268,21 +266,28 @@ class CTkComboBox(CTkBaseClass): outline=ThemeManager.single_color(self.button_color, self._appearance_mode), fill=ThemeManager.single_color(self.button_color, self._appearance_mode)) - def set(self, value: str, from_variable_callback: bool = False, from_dropdown_menu_callback: bool = False): - self.current_value = value - + def dropdown_callback(self, value: str): if self.state == "readonly": self.entry.configure(state="normal") self.entry.delete(0, tkinter.END) - self.entry.insert(0, self.current_value) + self.entry.insert(0, value) self.entry.configure(state="readonly") else: self.entry.delete(0, tkinter.END) - self.entry.insert(0, self.current_value) + self.entry.insert(0, value) - if from_dropdown_menu_callback: - if self.command is not None: - self.command(self.current_value) + if self.command is not None: + self.command(value) + + def set(self, value: str): + if self.state == "readonly": + self.entry.configure(state="normal") + self.entry.delete(0, tkinter.END) + self.entry.insert(0, value) + self.entry.configure(state="readonly") + else: + self.entry.delete(0, tkinter.END) + self.entry.insert(0, value) def get(self) -> str: return self.entry.get() diff --git a/customtkinter/widgets/ctk_optionmenu.py b/customtkinter/widgets/ctk_optionmenu.py index c290fff..e05e054 100644 --- a/customtkinter/widgets/ctk_optionmenu.py +++ b/customtkinter/widgets/ctk_optionmenu.py @@ -72,7 +72,7 @@ class CTkOptionMenu(CTkBaseClass): self.dropdown_menu = DropdownMenu(master=self, values=self.values, - command=self.set, + command=self.dropdown_callback, fg_color=dropdown_color, hover_color=dropdown_hover_color, text_color=dropdown_text_color, @@ -124,7 +124,8 @@ class CTkOptionMenu(CTkBaseClass): if self.variable is not None: self.variable_callback_name = self.variable.trace_add("write", self.variable_callback) - self.set(self.variable.get(), from_variable_callback=True) + self.current_value = self.variable.get() + self.text_label.configure(text=self.current_value) def set_scaling(self, *args, **kwargs): super().set_scaling(*args, **kwargs) @@ -225,7 +226,7 @@ class CTkOptionMenu(CTkBaseClass): if self.variable is not None and self.variable != "": self.variable_callback_name = self.variable.trace_add("write", self.variable_callback) - self.set(self.variable.get(), from_variable_callback=True) + self.set(self.variable.get(), block_set_variable=True) else: self.variable = None @@ -277,21 +278,29 @@ class CTkOptionMenu(CTkBaseClass): def variable_callback(self, var_name, index, mode): if not self.variable_callback_blocked: - self.set(self.variable.get(), from_variable_callback=True) + self.current_value = self.variable.get() + self.text_label.configure(text=self.current_value) - def set(self, value: str, from_variable_callback: bool = False, from_dropdown_menu_callback: bool = False): + def dropdown_callback(self, value: str): self.current_value = value - self.text_label.configure(text=self.current_value) - if self.variable is not None and not from_variable_callback: + if self.variable is not None: self.variable_callback_blocked = True self.variable.set(self.current_value) self.variable_callback_blocked = False - if from_dropdown_menu_callback: - if self.command is not None: - self.command(self.current_value) + if self.command is not None: + self.command(self.current_value) + + def set(self, value: str): + self.current_value = value + self.text_label.configure(text=self.current_value) + + if self.variable is not None: + self.variable_callback_blocked = True + self.variable.set(self.current_value) + self.variable_callback_blocked = False def get(self) -> str: return self.current_value diff --git a/customtkinter/widgets/dropdown_menu.py b/customtkinter/widgets/dropdown_menu.py index 57c5af8..a553c47 100644 --- a/customtkinter/widgets/dropdown_menu.py +++ b/customtkinter/widgets/dropdown_menu.py @@ -96,7 +96,7 @@ class DropdownMenu(tkinter.Menu): def button_callback(self, value): if self.command is not None: - self.command(value, from_dropdown_menu_callback=True) + self.command(value) def configure(self, **kwargs): if "values" in kwargs: diff --git a/test/manual_integration_tests/complex_example_new.py b/test/manual_integration_tests/complex_example_new.py index 53d1854..e6ef785 100644 --- a/test/manual_integration_tests/complex_example_new.py +++ b/test/manual_integration_tests/complex_example_new.py @@ -87,7 +87,7 @@ class App(customtkinter.CTk): self.checkbox_1.grid(row=1, column=0, pady=(20, 10), padx=20, sticky="n") self.checkbox_2 = customtkinter.CTkCheckBox(master=self.checkbox_slider_frame) self.checkbox_2.grid(row=2, column=0, pady=10, padx=20, sticky="n") - self.switch_1 = customtkinter.CTkSwitch(master=self.checkbox_slider_frame) + self.switch_1 = customtkinter.CTkSwitch(master=self.checkbox_slider_frame, command=lambda: print("switch 1 toggle")) self.switch_1.grid(row=3, column=0, pady=10, padx=20, sticky="n") self.switch_2 = customtkinter.CTkSwitch(master=self.checkbox_slider_frame) self.switch_2.grid(row=4, column=0, pady=(10, 20), padx=20, sticky="n")