Akascape / CTkListbox

A simple listbox for customtkinter (extenstion/add-on)
MIT License
130 stars 14 forks source link

Select event bug #45

Closed infiniteTesch closed 4 months ago

infiniteTesch commented 5 months ago

I'm facing a perplexing issue that's beyond my current skill level, and I'm hoping for some expert insight. In my project, I'm struggling with managing multiple listboxes, each with distinct select events. Oddly, regardless of whether a specific class is instantiated, selecting an item in any listbox triggers the event linked to the first-created listbox. This behavior persists even though it seems like a fundamental error in my part.

I've spent considerable time troubleshooting but to no avail. It seems like there might be some underlying issue that I'm not equipped to solve. That's where I hope you, the brilliant minds of this community, can come in. Akash, or any other experienced contributor, your expertise on the deeper architecture of this functionality would be invaluable.

To clarify the situation:

EditorListbox(CTkListbox) is in class A with its unique on_click function. RandomListbox(CTkListbox) is in class B, also with its own on_click function. Despite EditorListbox not being active (its parent class isn't even instantiated), selecting an item in RandomListbox – which is entirely out of scope – somehow triggers the function connected to EditorListbox.

This behavior is baffling and is a significant roadblock in my project. Any insights, advice, or potential solutions would be greatly appreciated. I'm eager to learn and resolve this issue, and your help could be a turning point.

Thank you in advance for your time and expertise!

The code:

self.listbox = EditorListbox(self, command=self.on_listbox_select)

class EditorListbox(CTkListbox):
    def __init__(self, master, width=200, height=400, fg_color=listbox_fg_color, select_color=listbox_select_color, text_color=listbox_txt_color, font=None, border_color=listbox_border_color, hover_color=hver_color, scrollbar_button_color=scrollbar_color, scrollbar_button_hover_color=scrollbar_hover_color, command=None, **kwargs):
        if font is None:
            font = tk.CTkFont(default_font, size=txt_size)
        super().__init__(master, width=width, height=height, fg_color=fg_color, highlight_color=select_color, text_color=text_color, font=font, border_color=border_color, hover_color=hover_color, scrollbar_button_color=scrollbar_button_color, scrollbar_button_hover_color=scrollbar_button_hover_color, **kwargs)

        self.command = command
        self.bind("<<ListboxSelect>>", self.on_listbox_select)

    def update_theme(self):
        self.configure(
            fg_color=listbox_fg_color, 
            highlight_color=listbox_select_color,  
            border_color=listbox_border_color, 
            hover_color=listbox_select_color,
            text_color = listbox_txt_color,
            scrollbar_button_color=scrollbar_color,
            scrollbar_button_hover_color=scrollbar_hover_color
        )

    def on_listbox_select(self, event):
        if self.command:
            self.command(event)
infiniteTesch commented 5 months ago
def bind(self, key, func):
    super().bind_all(key, lambda e: func(self.get()), add="+")

def unbind(self, key):
    super().unbind_all(key)

added the unbind function the the CTkListbox class and tried to call it from my widget class:

    self.unbind("<<ListboxSelect>>")
    self.bind("<<ListboxSelect>>", self.on_listbox_selects)  

  This removed the connection to EditorListbox, but did not succesfully re-bind.