Search code examples
pythontkinterlistboxtkinter-entry

Tkinter Listbox with Entry


Is there a way to have the items of a Tkinter Listbox be Entry Widgets? The result would be that you could dynamically modify the text in an Listbox entry. If your Listbox looks like:

 --------
| Apples  |
| Pears   |
| Oranges |
 ---------

then you would want to be able to click on Apples and write some arbitrary text - you could then bind the Enter key, say, to trigger a function based on the new text.


Solution

  • you could give the user some entries then create a listbox from that input

    but you cant just change a listboxes text like that

    maybe try a different GUI Library like WX

    EDIT

    here is something you can do:

    from Tkinter import *
    
    
    root = Tk()
    opt_list = ['opt1','opt2','opt3','opt4','opt5']
    sel_list = []
    
    def get_sel():
        sel_list.append(Lb1.curselection())
        root.destroy()
    
    def change_opt():
        entry = E.get()
        change = entry.split(" ")
        print change
        Lb1.insert(int(change[0]),change[1])
        root.update()
    
    
    def cancel():
        root.destroy()
    E = Entry(root)
    A = Button(root, text ="Change", command = change_opt)
    B = Button(root, text ="Submit", command = get_sel)
    C = Button(root, text ="Cancel", command = cancel)
    Lb1 = Listbox(root, selectmode=MULTIPLE)
    
    
    for i,j in enumerate(opt_list):
        Lb1.insert(i,j)
    
    
    Lb1.pack()
    B.pack()
    C.pack()
    E.pack()
    A.pack()
    
    root.mainloop()
    

    this will make a listbox with the options in opt_list then when you type for example 5 hello the entry and press Change it will add the option hello to the fifth place

    thats the only way i can think of