Listbox in Tkinter python

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2006
Posts: 25
Reputation: wandie is an unknown quantity at this point 
Solved Threads: 0
wandie wandie is offline Offline
Light Poster

Listbox in Tkinter python

 
0
  #1
Dec 27th, 2006
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
  1. musicfolder = [
  2. ["CollegeRock/"],
  3. ['RnB/'],
  4. ['HipHop/'],
  5. ['Build/'],
  6. ['Buy/'],
  7. ['Techno/'],
  8. ['Jazz/'],
  9. ['Classic/']
  10. ]
this is the code i used to get the value and to display it in the Entrybox
  1. #get selected line index
  2. index = lb.curselection()[0]
  3. #print index
  4. seltext = lb.get(index)
  5. #get the line's text
  6. seltext = lb.get(index)
  7. #now display the selecred text
  8. E2.insert(0,seltext)
This is the error I recieve
  1. Traceback (most recent call last):
  2. File "C:\Documents and Settings\stage1\Desktop\List\List.py", line 88, in -toplevel-
  3. index = lb.curselection()[0]
  4. IndexError: tuple index out of range
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Listbox in Tkinter python

 
0
  #2
Dec 27th, 2006
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:
[php]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()
[/php]
Last edited by Ene Uran; Dec 27th, 2006 at 11:29 am.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 25
Reputation: wandie is an unknown quantity at this point 
Solved Threads: 0
wandie wandie is offline Offline
Light Poster

Re: Listbox in Tkinter python

 
0
  #3
Jan 2nd, 2007
thank you for the code. sorry for the delay.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2931 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC