ComboBox and DropDown fixes for Windows

This commit is contained in:
TomSchimansky 2022-06-02 00:15:24 +02:00
parent 550653c6c3
commit 807064a888
4 changed files with 25 additions and 18 deletions

View File

@ -113,10 +113,6 @@ class CTkComboBox(CTkBaseClass):
if self.variable is not None:
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)
@ -169,8 +165,12 @@ class CTkComboBox(CTkBaseClass):
if self.state == tkinter.DISABLED:
self.entry.configure(fg=(ThemeManager.single_color(self.text_color_disabled, self._appearance_mode)))
self.canvas.itemconfig("dropdown_arrow",
fill=ThemeManager.single_color(self.text_color_disabled, self._appearance_mode))
else:
self.entry.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
self.canvas.itemconfig("dropdown_arrow",
fill=ThemeManager.single_color(self.text_color, self._appearance_mode))
def open_dropdown_menu(self):
self.dropdown_menu = DropdownMenu(x_position=self.winfo_rootx(),

View File

@ -1,6 +1,7 @@
import customtkinter
import tkinter
import sys
from typing import Union
from ..theme_manager import ThemeManager
from ..appearance_mode_tracker import AppearanceModeTracker
@ -27,7 +28,8 @@ class DropdownMenu(tkinter.Toplevel):
super().__init__(*args, **kwargs)
ScalingTracker.add_widget(self.set_scaling, self)
self.widget_scaling = ScalingTracker.get_widget_scaling(self)
self._widget_scaling = ScalingTracker.get_widget_scaling(self)
self._spacing_scaling = ScalingTracker.get_spacing_scaling(self)
self.values = values
self.command = command
@ -44,12 +46,13 @@ class DropdownMenu(tkinter.Toplevel):
self.button_corner_radius = button_corner_radius
self.button_height = button_height
self.width = width
self.height = max(len(self.values), 1) * (self.button_height + y_spacing) + y_spacing
self.height = max(len(self.values), 1) * (self.button_height + self.apply_spacing_scaling(y_spacing)) + self.apply_spacing_scaling(y_spacing)
self.geometry(f"{round(self.apply_widget_scaling(self.width))}x" +
f"{round(self.apply_widget_scaling(self.height))}+" +
f"{round(x_position)}+{round(y_position)}")
self.grid_columnconfigure(0, weight=1)
self.grid_rowconfigure(0, weight=1)
if sys.platform.startswith("darwin"):
self.overrideredirect(True) # remove title-bar
@ -82,8 +85,8 @@ class DropdownMenu(tkinter.Toplevel):
corner_radius=self.corner_radius,
fg_color=self.fg_color, overwrite_preferred_drawing_method="circle_shapes")
self.frame.grid(row=0, column=0, sticky="nsew", rowspan=len(self.values) + 1)
self.frame.grid_rowconfigure(len(self.values) + 1, minsize=y_spacing) # add spacing at the bottom
self.frame.grid(row=0, column=0, sticky="nsew", rowspan=1)
self.frame.grid_rowconfigure(len(self.values) + 1, minsize=self.apply_spacing_scaling(y_spacing)) # add spacing at the bottom
self.frame.grid_columnconfigure(0, weight=1)
self.button_list = []
@ -91,7 +94,7 @@ class DropdownMenu(tkinter.Toplevel):
button = customtkinter.CTkButton(self.frame,
text=option,
height=self.button_height,
width=self.width - 2 * x_spacing,
width=self.width - 2 * self.apply_widget_scaling(x_spacing),
fg_color=self.button_color,
text_color=self.text_color,
hover_color=self.button_hover_color,
@ -100,16 +103,22 @@ class DropdownMenu(tkinter.Toplevel):
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,
pady=(y_spacing, 0), sticky="ew")
padx=self.apply_widget_scaling(x_spacing),
pady=(self.apply_widget_scaling(y_spacing), 0), sticky="ew")
self.button_list.append(button)
self.bind("<FocusOut>", self.focus_loss_event)
self.frame.canvas.bind("<Button-1>", self.focus_loss_event)
def apply_widget_scaling(self, value):
def apply_widget_scaling(self, value: Union[int, float, str]) -> Union[float, str]:
if isinstance(value, (int, float)):
return value * self.widget_scaling
return value * self._widget_scaling
else:
return value
def apply_spacing_scaling(self, value: Union[int, float, str]) -> Union[float, str]:
if isinstance(value, (int, float)):
return value * self._spacing_scaling
else:
return value

View File

@ -36,13 +36,11 @@ slider_1.set(0.5)
entry_1 = customtkinter.CTkEntry(master=frame_1, placeholder_text="CTkEntry")
entry_1.pack(pady=12, padx=10)
s = customtkinter.StringVar(value="test")
optionmenu_1 = customtkinter.CTkOptionMenu(frame_1, values=["Option 1", "Option 2", "Option 42"], variable=s)
optionmenu_1 = customtkinter.CTkOptionMenu(frame_1, values=["Option 1", "Option 2", "Option 42"])
optionmenu_1.pack(pady=12, padx=10)
optionmenu_1.set("CTkOptionMenu")
combobox_1 = customtkinter.CTkComboBox(frame_1, values=["Option 1", "Option 2", "Option 42"], variable=s)
combobox_1 = customtkinter.CTkComboBox(frame_1, values=["Option 1", "Option 2", "Option 42"])
combobox_1.pack(pady=12, padx=10)
optionmenu_1.set("CTkComboBox")

View File

@ -11,7 +11,7 @@ app.title("Tkinter Variable Test")
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 wjkfjdshkjfb")
txt_var.set("new text test")
if TEST_CONFIGURE: entry_1.configure(textvariable=txt_var)
if TEST_REMOVING: entry_1.configure(textvariable="")