small fixes for CTkComboBox

This commit is contained in:
Tom Schimansky
2022-06-01 23:50:50 +02:00
parent 0aa9dfc70f
commit 550653c6c3
8 changed files with 80 additions and 86 deletions

View File

@ -61,8 +61,6 @@ class CTkComboBox(CTkBaseClass):
# callback and hover functionality
self.function = command
self.variable = variable
self.variable_callback_blocked = False
self.variable_callback_name = None
self.state = state
self.hover = hover
self.click_animation_running = False
@ -91,12 +89,13 @@ class CTkComboBox(CTkBaseClass):
self.draw_engine = DrawEngine(self.canvas)
self.entry = tkinter.Entry(master=self,
width=0,
state=self.state,
width=1,
bd=0,
highlightthickness=0,
font=self.apply_font_scaling(self.text_font))
left_section_width = self._current_width - self._current_height
self.entry.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="w",
self.entry.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="ew",
padx=(self.apply_widget_scaling(max(self.corner_radius, 3)),
self.apply_widget_scaling(max(self._current_width - left_section_width + 3, 3))))
@ -112,16 +111,15 @@ class CTkComboBox(CTkBaseClass):
self.bind('<Configure>', self.update_dimensions_event)
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.entry.configure(textvariable=self.variable)
# 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)
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
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
height=self.apply_widget_scaling(self._desired_height))
self.draw()
@ -190,7 +188,7 @@ class CTkComboBox(CTkBaseClass):
if "state" in kwargs:
self.state = kwargs["state"]
self.set_cursor()
self.entry.configure(state=self.state)
require_redraw = True
del kwargs["state"]
@ -227,17 +225,8 @@ class CTkComboBox(CTkBaseClass):
del kwargs["command"]
if "variable" in kwargs:
if self.variable is not None: # remove old callback
self.variable.trace_remove("write", self.variable_callback_name)
self.variable = kwargs["variable"]
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)
else:
self.variable = None
self.entry.configure(textvariable=self.variable)
del kwargs["variable"]
if "width" in kwargs:
@ -248,13 +237,17 @@ class CTkComboBox(CTkBaseClass):
self.set_dimensions(height=kwargs["height"])
del kwargs["height"]
if "values" in kwargs:
self.values = kwargs["values"]
del kwargs["values"]
super().configure(*args, **kwargs)
if require_redraw:
self.draw()
def on_enter(self, event=0):
if self.hover is True and self.state == tkinter.NORMAL:
if self.hover is True and self.state == tkinter.NORMAL and len(self.values) > 0:
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
self.canvas.configure(cursor="pointinghand")
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
@ -289,30 +282,21 @@ class CTkComboBox(CTkBaseClass):
if self.click_animation_running:
self.on_enter()
def variable_callback(self, var_name, index, mode):
if not self.variable_callback_blocked:
self.set(self.variable.get(), from_variable_callback=True)
def set(self, value: str, from_variable_callback: bool = False):
self.current_value = value
self.entry.delete(0, tkinter.END)
self.entry.insert(0, self.current_value)
if self.variable is not None and not from_variable_callback:
self.variable_callback_blocked = True
self.variable.set(self.current_value)
self.variable_callback_blocked = False
if not from_variable_callback:
if self.function is not None:
self.function(self.current_value)
def get(self) -> str:
return self.current_value
return self.entry.get()
def clicked(self, event=0):
if self.state is not tkinter.DISABLED:
if self.state is not tkinter.DISABLED and len(self.values) > 0:
self.open_dropdown_menu()
# click animation: change color with .on_leave() and back to normal after 100ms with click_animation()

View File

@ -93,7 +93,6 @@ class CTkOptionMenu(CTkBaseClass):
self.canvas.bind("<Button-1>", self.clicked)
self.bind('<Configure>', self.update_dimensions_event)
self.set_cursor()
self.draw() # initial draw
if self.variable is not None:
@ -169,23 +168,21 @@ class CTkOptionMenu(CTkBaseClass):
self.text_label.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode))
def open_dropdown_menu(self):
if len(self.values) > 0:
self.dropdown_menu = DropdownMenu(x_position=self.winfo_rootx(),
y_position=self.winfo_rooty() + self.apply_widget_scaling(self._current_height + 4),
width=self._current_width,
values=self.values,
command=self.set,
fg_color=self.dropdown_color,
button_hover_color=self.dropdown_hover_color,
button_color=self.dropdown_color,
text_color=self.dropdown_text_color)
self.dropdown_menu = DropdownMenu(x_position=self.winfo_rootx(),
y_position=self.winfo_rooty() + self.apply_widget_scaling(self._current_height + 4),
width=self._current_width,
values=self.values,
command=self.set,
fg_color=self.dropdown_color,
button_hover_color=self.dropdown_hover_color,
button_color=self.dropdown_color,
text_color=self.dropdown_text_color)
def configure(self, *args, **kwargs):
require_redraw = False # some attribute changes require a call of self.draw() at the end
if "state" in kwargs:
self.state = kwargs["state"]
self.set_cursor()
require_redraw = True
del kwargs["state"]
@ -246,29 +243,19 @@ class CTkOptionMenu(CTkBaseClass):
if "values" in kwargs:
self.values = kwargs["values"]
del kwargs["values"]
self.set_cursor()
super().configure(*args, **kwargs)
if require_redraw:
self.draw()
def set_cursor(self):
if Settings.cursor_manipulation_enabled:
if self.state == tkinter.DISABLED:
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
self.configure(cursor="arrow")
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
self.configure(cursor="arrow")
elif self.state == tkinter.NORMAL:
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
self.configure(cursor="pointinghand")
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
self.configure(cursor="hand2")
def on_enter(self, event=0):
if self.hover is True and self.state == tkinter.NORMAL:
if self.hover is True and self.state == tkinter.NORMAL and len(self.values) > 0:
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
self.configure(cursor="pointinghand")
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
self.configure(cursor="hand2")
# set color of inner button parts to hover color
self.canvas.itemconfig("inner_parts_right",
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
@ -278,6 +265,11 @@ class CTkOptionMenu(CTkBaseClass):
self.click_animation_running = False
if self.hover is True:
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
self.configure(cursor="arrow")
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
self.configure(cursor="arrow")
# set color of inner button parts
self.canvas.itemconfig("inner_parts_right",
outline=ThemeManager.single_color(self.button_color, self._appearance_mode),
@ -312,7 +304,7 @@ class CTkOptionMenu(CTkBaseClass):
return self.current_value
def clicked(self, event=0):
if self.state is not tkinter.DISABLED:
if self.state is not tkinter.DISABLED and len(self.values) > 0:
self.open_dropdown_menu()
# click animation: change color with .on_leave() and back to normal after 100ms with click_animation()

View File

@ -97,6 +97,7 @@ class DropdownMenu(tkinter.Toplevel):
hover_color=self.button_hover_color,
corner_radius=self.button_corner_radius,
command=lambda i=index: self.button_callback(i))
button.text_label.configure(anchor="w")
button.text_label.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="w")
button.grid(row=index, column=0,
padx=x_spacing,