fixed optionmenu and combobox bugs

This commit is contained in:
Tom Schimansky 2022-06-23 21:05:44 +02:00
parent ec3fdc40ff
commit d4ae8cab7d
4 changed files with 42 additions and 41 deletions

View File

@ -398,8 +398,8 @@ class DrawEngine:
if not self._canvas.find_withtag("border_parts"): if not self._canvas.find_withtag("border_parts"):
self._canvas.create_polygon((0, 0, 0, 0), tags=("border_line_left_1", "border_parts_left", "border_parts", "left_parts")) self._canvas.create_polygon((0, 0, 0, 0), tags=("border_line_left_1", "border_parts_left", "border_parts", "left_parts"))
self._canvas.create_polygon((0, 0, 0, 0), tags=("border_line_right_1", "border_parts_right", "border_parts", "right_parts")) self._canvas.create_polygon((0, 0, 0, 0), tags=("border_line_right_1", "border_parts_right", "border_parts", "right_parts"))
self._canvas.create_rectangle((0, 0, 0, 0), tags=("border_rect_left_1", "border_parts_left", "border_parts", "left_parts")) self._canvas.create_rectangle((0, 0, 0, 0), tags=("border_rect_left_1", "border_parts_left", "border_parts", "left_parts"), width=0)
self._canvas.create_rectangle((0, 0, 0, 0), tags=("border_rect_right_1", "border_parts_right", "border_parts", "right_parts")) self._canvas.create_rectangle((0, 0, 0, 0), tags=("border_rect_right_1", "border_parts_right", "border_parts", "right_parts"), width=0)
requires_recoloring = True requires_recoloring = True
self._canvas.coords("border_line_left_1", self._canvas.coords("border_line_left_1",

View File

@ -100,6 +100,9 @@ class CTkComboBox(CTkBaseClass):
padx=(max(self.apply_widget_scaling(self.corner_radius), self.apply_widget_scaling(3)), 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)))) 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)
self.draw() # initial draw self.draw() # initial draw
# event bindings # event bindings
@ -140,10 +143,6 @@ class CTkComboBox(CTkBaseClass):
self.apply_widget_scaling(self._current_height / 2), self.apply_widget_scaling(self._current_height / 2),
self.apply_widget_scaling(self._current_height / 3)) self.apply_widget_scaling(self._current_height / 3))
if self.current_value is not None:
self.entry.delete(0, tkinter.END)
self.entry.insert(0, self.current_value)
if no_color_updates is False or requires_recoloring or requires_recoloring_2: 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)) self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))

View File

@ -1,6 +1,5 @@
import tkinter import tkinter
import sys import sys
from typing import Union
from .dropdown_menu import DropdownMenu from .dropdown_menu import DropdownMenu
@ -32,6 +31,7 @@ class CTkOptionMenu(CTkBaseClass):
dropdown_text_font="default_theme", dropdown_text_font="default_theme",
hover=True, hover=True,
state=tkinter.NORMAL, state=tkinter.NORMAL,
dynamic_resizing=True,
**kwargs): **kwargs):
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass # transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
@ -58,6 +58,7 @@ class CTkOptionMenu(CTkBaseClass):
self.variable_callback_name = None self.variable_callback_name = None
self.state = state self.state = state
self.hover = hover self.hover = hover
self.dynamic_resizing = dynamic_resizing
if values is None: if values is None:
self.values = ["CTkOptionMenu"] self.values = ["CTkOptionMenu"]
@ -89,11 +90,14 @@ class CTkOptionMenu(CTkBaseClass):
self.draw_engine = DrawEngine(self.canvas) self.draw_engine = DrawEngine(self.canvas)
left_section_width = self._current_width - self._current_height left_section_width = self._current_width - self._current_height
self.text_label = tkinter.Label(master=self, font=self.apply_font_scaling(self.text_font)) self.text_label = tkinter.Label(master=self, font=self.apply_font_scaling(self.text_font), anchor="w")
self.text_label.grid(row=0, column=0, sticky="w", self.text_label.grid(row=0, column=0, sticky="w",
padx=(max(self.apply_widget_scaling(self.corner_radius), self.apply_widget_scaling(3)), 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)))) max(self.apply_widget_scaling(self._current_width - left_section_width + 3), self.apply_widget_scaling(3))))
if not self.dynamic_resizing:
self.grid_propagate(0)
if Settings.cursor_manipulation_enabled: if Settings.cursor_manipulation_enabled:
if sys.platform == "darwin": if sys.platform == "darwin":
self.configure(cursor="pointinghand") self.configure(cursor="pointinghand")
@ -176,6 +180,8 @@ class CTkOptionMenu(CTkBaseClass):
self.text_label.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode)) self.text_label.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode))
self.canvas.update_idletasks()
def open_dropdown_menu(self): def open_dropdown_menu(self):
self.dropdown_menu.open(self.winfo_rootx(), self.dropdown_menu.open(self.winfo_rootx(),
self.winfo_rooty() + self.apply_widget_scaling(self._current_height + 0)) self.winfo_rooty() + self.apply_widget_scaling(self._current_height + 0))
@ -184,47 +190,41 @@ class CTkOptionMenu(CTkBaseClass):
require_redraw = False # some attribute changes require a call of self.draw() at the end require_redraw = False # some attribute changes require a call of self.draw() at the end
if "state" in kwargs: if "state" in kwargs:
self.state = kwargs["state"] self.state = kwargs.pop("state")
require_redraw = True require_redraw = True
del kwargs["state"]
if "fg_color" in kwargs: if "fg_color" in kwargs:
self.fg_color = kwargs["fg_color"] self.fg_color = kwargs.pop("fg_color")
require_redraw = True require_redraw = True
del kwargs["fg_color"]
if "bg_color" in kwargs: 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() self.bg_color = self.detect_color_of_master()
else: else:
self.bg_color = kwargs["bg_color"] self.bg_color = new_bg_color
require_redraw = True require_redraw = True
del kwargs["bg_color"]
if "button_color" in kwargs: if "button_color" in kwargs:
self.button_color = kwargs["button_color"] self.button_color = kwargs.pop("button_color")
require_redraw = True require_redraw = True
del kwargs["button_color"]
if "button_hover_color" in kwargs: 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 require_redraw = True
del kwargs["button_hover_color"]
if "text_color" in kwargs: if "text_color" in kwargs:
self.text_color = kwargs["text_color"] self.text_color = kwargs.pop("text_color")
require_redraw = True require_redraw = True
del kwargs["text_color"]
if "command" in kwargs: if "command" in kwargs:
self.function = kwargs["command"] self.function = kwargs.pop("command")
del kwargs["command"]
if "variable" in kwargs: if "variable" in kwargs:
if self.variable is not None: # remove old callback if self.variable is not None: # remove old callback
self.variable.trace_remove("write", self.variable_callback_name) self.variable.trace_remove("write", self.variable_callback_name)
self.variable = kwargs["variable"] self.variable = kwargs.pop("variable")
if self.variable is not None and self.variable != "": if self.variable is not None and self.variable != "":
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback) self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
@ -232,36 +232,34 @@ class CTkOptionMenu(CTkBaseClass):
else: else:
self.variable = None self.variable = None
del kwargs["variable"]
if "width" in kwargs: if "width" in kwargs:
self.set_dimensions(width=kwargs["width"]) self.set_dimensions(width=kwargs.pop("width"))
del kwargs["width"]
if "height" in kwargs: if "height" in kwargs:
self.set_dimensions(height=kwargs["height"]) self.set_dimensions(height=kwargs.pop("height"))
del kwargs["height"]
if "values" in kwargs: if "values" in kwargs:
self.values = kwargs["values"] self.values = kwargs.pop("values")
del kwargs["values"]
self.dropdown_menu.configure(values=self.values) self.dropdown_menu.configure(values=self.values)
if "dropdown_color" in kwargs: if "dropdown_color" in kwargs:
self.dropdown_menu.configure(fg_color=kwargs["dropdown_color"]) self.dropdown_menu.configure(fg_color=kwargs.pop("dropdown_color"))
del kwargs["dropdown_color"]
if "dropdown_hover_color" in kwargs: if "dropdown_hover_color" in kwargs:
self.dropdown_menu.configure(hover_color=kwargs["dropdown_hover_color"]) self.dropdown_menu.configure(hover_color=kwargs.pop("dropdown_hover_color"))
del kwargs["dropdown_hover_color"]
if "dropdown_text_color" in kwargs: if "dropdown_text_color" in kwargs:
self.dropdown_menu.configure(text_color=kwargs["dropdown_text_color"]) self.dropdown_menu.configure(text_color=kwargs.pop("dropdown_text_color"))
del kwargs["dropdown_text_color"]
if "dropdown_text_font" in kwargs: if "dropdown_text_font" in kwargs:
self.dropdown_menu.configure(text_font=kwargs["dropdown_text_font"]) self.dropdown_menu.configure(text_font=kwargs.pop("dropdown_text_font"))
del kwargs["dropdown_text_font"]
if "dynamic_resizing" in kwargs:
self.dynamic_resizing = kwargs.pop("dynamic_resizing")
if not self.dynamic_resizing:
self.grid_propagate(0)
else:
self.grid_propagate(1)
super().configure(*args, **kwargs) super().configure(*args, **kwargs)

View File

@ -23,10 +23,14 @@ optionmenu_tk.pack(pady=10, padx=10)
optionmenu_1 = customtkinter.CTkOptionMenu(app, variable=variable, values=countries, command=select_callback) optionmenu_1 = customtkinter.CTkOptionMenu(app, variable=variable, values=countries, command=select_callback)
optionmenu_1.pack(pady=20, padx=10) optionmenu_1.pack(pady=20, padx=10)
optionmenu_2 = customtkinter.CTkOptionMenu(app, variable=variable, values=countries, command=select_callback,
dynamic_resizing=False)
optionmenu_2.pack(pady=20, padx=10)
combobox_tk = ttk.Combobox(app, values=countries) combobox_tk = ttk.Combobox(app, values=countries)
combobox_tk.pack(pady=10, padx=10) combobox_tk.pack(pady=10, padx=10)
combobox_1 = customtkinter.CTkComboBox(app, variable=variable, values=countries, command=select_callback) combobox_1 = customtkinter.CTkComboBox(app, variable=None, values=countries, command=select_callback, width=300)
combobox_1.pack(pady=20, padx=10) combobox_1.pack(pady=20, padx=10)
app.mainloop() app.mainloop()