Akascape / CTkScrollableDropdown

Easily add a new scrollable dropdown menu in customtkinter widgets (extension/add-on)
MIT License
112 stars 11 forks source link

2 Bugs - Font bug and Custom String Var bug #31

Open Annajiraochalla opened 7 months ago

Annajiraochalla commented 7 months ago

Hi @Akascape,

Here is a short example using CTkScrollableDropdown

from CTkScrollableDropdown import *
import customtkinter

root = customtkinter.CTk()

customtkinter.CTkLabel(root, text="Select your favorite month").pack(pady=5)

values = ['January', 'February', 'March', 'April', 
          'May', 'June', 'July', 'August', 'September', 
          'October', 'November', 'December']

variable = customtkinter.StringVar(value = "Month")
combobox = customtkinter.CTkComboBox(root, variable = variable, width=240)
combobox.pack(fill="x", padx=10, pady=10)

CTkScrollableDropdown(combobox, values=values, justify="left", 
                      button_color="transparent", 
                      autocomplete=True, font = ("Consolas", 12))

root.mainloop()

bug 1: Even though we are able to define custom font through **button_kwargs, while performing search operation, the font of the buttons revokes to default font.

bug 2: Generally, we can define custom string var for CTkComboBox and CTkOptionMenu but the CTkScrollableDropdown is kind of overwriting it with the values[0].

Annajiraochalla commented 7 months ago

The following solution worked for me. @Akascape, please check.

-> Fix for bug 1:

Add button_font in the init method of CTkScrollableDropdown

def __init__(self, attach, x=None, y=None, button_color=None, height: int = 200, width: int = None,
                 fg_color=None, button_height: int = 20, justify="center", scrollbar_button_color=None,
                 scrollbar=True, scrollbar_button_hover_color=None, frame_border_width=2, values=[],
                 command=None, image_values=[], alpha: float = 0.97, frame_corner_radius=20, double_click=False,
                 resize=True, frame_border_color=None, text_color=None, autocomplete=False, button_font = None,
                 hover_color=None, **button_kwargs): 

                self.button_font = button_font

In _init_buttons() method, add the font parameter in CTkButton

self.widgets[self.i] = customtkinter.CTkButton(self.frame,
                                                  text=row,
                                                  height=self.button_height,
                                                  fg_color=self.button_color,
                                                  text_color=self.text_color,
                                                  image=self.image_values[self.i] if self.image_values is not None else None,
                                                  anchor=self.justify,
                                                  hover_color = self.hover_color,
                                                  font = self.button_font,
                                                  command=lambda k=row: self._attach_key_press(k), **button_kwargs)

Add the following if statement in configure() method

if "button_font" in kwargs:
    self.button_font = kwargs.pop("button_font")

-> Fix for bug 2:

Add placeholder_text in the init method of CTkScrollableDropdown

class CTkScrollableDropdown(customtkinter.CTkToplevel):

def __init__(self, attach, placeholder_text = "", x=None, y=None, button_color=None, height: int = 200, width: int = None,
                 fg_color=None, button_height: int = 20, justify="center", scrollbar_button_color=None,
                 scrollbar=True, scrollbar_button_hover_color=None, frame_border_width=2, values=[],
                 command=None, image_values=[], alpha: float = 0.97, frame_corner_radius=20, double_click=False,
                 resize=True, frame_border_color=None, text_color=None, autocomplete=False, button_font = None,
                 hover_color=None, **button_kwargs): 

                self.placeholder_text = placeholder_text

In bind_autocomplete() method, change the parameter of self.attach.set()

self.attach.set(self.placeholder_text) #add this line at !ctkcombobox

Add the following if statement in configure() method

if "placeholder_text" not in kwargs:
    kwargs["placeholder_text"] = self.placeholder_text

Example:

CTkScrollableDropdown(month, values = values, placeholder_text = 'Month', justify="left", autocomplete=True, fg_color = 'white',
                        frame_corner_radius = 0, frame_border_width = 1, button_font = ('Segoe UI Light', 20),
                        scrollbar_button_color = '#74d4f7', scrollbar_button_hover_color = '#00B0F0', corner_radius = 0,
                        button_color = 'white', button_height = 40, hover_color= '#74d4f7', hover = True)