Akascape / CTkListbox

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

Behaviour issues while getting selection callbacks #2

Closed odavidsons closed 1 year ago

odavidsons commented 1 year ago

Hello! I've been using this addon alongside with CTK and i just stumbled apon some behaviour issues while trying to get callbacks when a list item is selected.

1 - While trying to bind a selection event ("listbox.bind("<>", callback)") it simply does not run. 2 - If i use the "command=" argument while declaring the listbox and point it to a function, i am able to get the selected item but only for the first time. After selecting an item for the second time the list refuses to visually change (and the selection actually remains stuck) and it only returns the last indexed item.

I did test the normal tkinter listbox and all is working as intended.

It could be incorrect implementation on my part, but some feedback on this would be aprecciated!

odavidsons commented 1 year ago

The example code i was experimenting with

import customtkinter as ctk
import tkinter as tk
from CTkListbox import *

def callback(event):
    selection = event.widget.curselection()
    if selection:
        index = selection[0]
        data = event.widget.get(index)
        print(data)

def ctkcallback(selected):
    print(selected)

app = tk.Tk()
ctk.set_appearance_mode("dark")
frame = ctk.CTkFrame(app)
frame.grid()
listbox = tk.Listbox(frame)
listbox.insert(tk.END,"teste1")
listbox.insert(tk.END,"teste2")
listbox.bind("<<ListboxSelect>>", callback)
listbox.grid(row=0,column=0)
ctklistbox = CTkListbox(frame,command=ctkcallback)
ctklistbox.insert("END","teste1")
ctklistbox.insert("END","teste2")
ctklistbox.grid(row=0,column=1)
app.mainloop()
Akascape commented 1 year ago

@odavidsons Ok, I figured it out, the problem is with "End" index.

odavidsons commented 1 year ago

Oh yeah, i see. Well, that is kind of an issue since i needed to dynamically add items to the last index of the list. Guess a workaround would need to be thought of for that

Akascape commented 1 year ago

@odavidsons Ok, its fixed now. You can now bind it like this: listbox.bind("<1>", func) The END method bug is also fixed.

odavidsons commented 1 year ago

Awesome! Thanks for the quick answer.