944,198 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 4661
  • Python RSS
Dec 27th, 2006
0

Listbox in Tkinter python

Expand Post »
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
Python Syntax (Toggle Plain Text)
  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
Python Syntax (Toggle Plain Text)
  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
Python Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
wandie is offline Offline
25 posts
since Dec 2006
Dec 27th, 2006
0

Re: Listbox in Tkinter python

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.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Jan 2nd, 2007
0

Re: Listbox in Tkinter python

thank you for the code. sorry for the delay.
Reputation Points: 10
Solved Threads: 0
Light Poster
wandie is offline Offline
25 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Button Binding Question:
Next Thread in Python Forum Timeline: Center a Tkinter Window





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC