Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default value bug #33

Open
ppoliziani opened this issue Apr 10, 2024 · 3 comments
Open

Default value bug #33

ppoliziani opened this issue Apr 10, 2024 · 3 comments

Comments

@ppoliziani
Copy link

ppoliziani commented Apr 10, 2024

Hi @Akascape,

I've noticed that when adding a command to the dropdown the command executes but the dropdown no longer updates showing the user which value was selected. For example:

from CTkScrollableDropdown import *
import customtkinter

root = customtkinter.CTk()
def hello(e):
    hellos = {"Italy": "Ciao",
              "Germany": "Hallo",
              "England": "Hello"}

    print(hellos[e])

countries = ["Italy", "Germany", "England"]

country_options = customtkinter.CTkComboBox(root)
country_options.pack()
CTkScrollableDropdown(attach=country_options, values=countries, autocomplete=True, alpha=1, command=hello)

root.mainloop()

This will print the correct hello for each selected language but the dropdown does not update to the chosen language.

@ppoliziani
Copy link
Author

ppoliziani commented Apr 11, 2024

Hi @Akascape,

A possible solution for the issue I mentioned yesterday is as follows. Kindly verify if this is a fix.

Problem:
When setting a command it seems as though the self.command variable in CTKScrollableDropdown is being occupied with the bounded method to the command a user passes. This means that when _init_buttons() attempts to initialise the options in the drop down, it was not bounding it to the command for the buttons that updates the dropdown.

Fix:
To fix this I created a new variable called method_command that a user would pass in their CTkScrollableDropdown object call to prevent the dropdown self.command variable from being bounded. And removed the command=None from the init method.

The init() method:

 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=[], **method_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, 
                 hover_color=None, bcommand=None, **button_kwargs):

# new variable
**self.method_command = method_command**

# updating command variable
self.command = None

The updated _attach_key_press() method is:

def _attach_key_press(self, k):
	self.event_generate("<<Selected>>")
	self.fade = True
	**if self.method_command:
		self.method_command(k)**
	if self.command:
		self.command(k)
	self.fade = False
	self.fade_out()
	self.withdraw()
	self.hide = True

The updated code from my inital comment would be:

from CTkScrollableDropdown import *
import customtkinter

root = customtkinter.CTk()
def hello(e):
    hellos = {"Italy": "Ciao",
              "Germany": "Hallo",
              "England": "Hello"}

    print(hellos[e])

countries = ["Italy", "Germany", "England"]

country_options = customtkinter.CTkComboBox(root)
country_options.pack()
CTkScrollableDropdown(attach=country_options, values=countries, autocomplete=True, alpha=1, **method_command=hello**)

root.mainloop()

This will now execute the command I've defined in my code and update the dropdown with the option I selected. Hopefully this helps :)

@JonnyNOS
Copy link

JonnyNOS commented May 2, 2024

Thanks, this fix works, but i recommend to put **if self.method_command: self.method_command(k)** after if self.command: self.command(k) so you get the now selected value and not the value selected before that

the _attach_key_press would look like this:

    def _attach_key_press(self, k):
        self.event_generate("<<Selected>>")
        self.fade = True
        if self.command:
            self.command(k)
        **if self.method_command:
            self.method_command(k)**
        self.fade = False
        self.fade_out()
        self.withdraw()
        self.hide = True

@Akascape
Copy link
Owner

Akascape commented Jun 29, 2024

But we can also simply use the .set method:

from CTkScrollableDropdown import *
import customtkinter

root = customtkinter.CTk()
def hello(e):
    hellos = {"Italy": "Ciao",
              "Germany": "Hallo",
              "England": "Hello"}

    print(hellos[e])
    country_options.set(e)
    
countries = ["Italy", "Germany", "England"]

country_options = customtkinter.CTkComboBox(root)
country_options.pack()
CTkScrollableDropdown(attach=country_options, values=countries, autocomplete=True, alpha=1, command=hello)

root.mainloop()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants