I have made a list of names and everytime i want to select a value in the listbox it should give me a result in the Entry box can someone please help me.

This is my listbox values

musicfolder = [
  ["CollegeRock/"],
  ['RnB/'],
  ['HipHop/'],
  ['Build/'],
  ['Buy/'],
  ['Techno/'],
  ['Jazz/'],
  ['Classic/']
]

this is the code i used to get the value and to display it in the Entrybox

#get selected line index
index = lb.curselection()[0]
#print index
seltext = lb.get(index)
#get the line's text
seltext = lb.get(index)
#now display the selecred text
E2.insert(0,seltext)

This is the error I recieve

Traceback (most recent call last):
  File "C:\Documents and Settings\stage1\Desktop\List\List.py", line 88, in -toplevel-
    index = lb.curselection()[0]
IndexError: tuple index out of range

You most likely get the error because you haven't selected anything on the listbox yet. You need to bind the listbox to the mouseclick that does the selection and to a function that contains your get(index) code. Something like this:

from Tkinter import *

musicfolder = [
  ["CollegeRock/"],
  ['RnB/'],
  ['HipHop/'],
  ['Build/'],
  ['Buy/'],
  ['Techno/'],
  ['Jazz/'],
  ['Classic/']
]

def get_list(event):
    """
    function to read the listbox selection
    and put the result in an entry widget
    """
    # get selected line index
    index = listbox1.curselection()[0]
    # get the line's text
    seltext = listbox1.get(index)
    # delete previous text in enter1
    enter1.delete(0, 50)
    # now display the selected text
    enter1.insert(0, seltext)
    
root = Tk()

# create the listbox (note that size is in characters)
listbox1 = Listbox(root, width=50, height=6)
listbox1.grid(row=0, column=0)

# create a vertical scrollbar to the right of the listbox
yscroll = Scrollbar(command=listbox1.yview, orient=VERTICAL)
yscroll.grid(row=0, column=1, sticky=N+S)
listbox1.configure(yscrollcommand=yscroll.set)

# create data entry
enter1 = Entry(root, width=50, bg='yellow')
enter1.insert(0, 'Click on an item in the listbox')
enter1.grid(row=1, column=0)

# load the listbox with data
for item in musicfolder:
    listbox1.insert(END, item)
 
# left mouse click on a list item to display selection
listbox1.bind('<ButtonRelease-1>', get_list)

root.mainloop()

thank you for the code. sorry for the delay.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.