| | |
Listbox in Tkinter python
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2006
Posts: 25
Reputation:
Solved Threads: 0
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
this is the code i used to get the value and to display it in the Entrybox
This is the error I recieve
This is my listbox values
Python Syntax (Toggle Plain Text)
musicfolder = [ ["CollegeRock/"], ['RnB/'], ['HipHop/'], ['Build/'], ['Buy/'], ['Techno/'], ['Jazz/'], ['Classic/'] ]
Python Syntax (Toggle Plain Text)
#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)
Python Syntax (Toggle Plain Text)
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:
[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]
[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
![]() |
Similar Threads
- Starting Python (Python)
- Help needed for displaying extended charactors in Tk() (Python)
- Tkinter user-input (Python)
- text adventure programming help (Python)
- XML advice (Python)
- Keeping Python window "on top" of others (Python)
- Need Help with Tkinter (Python)
Other Threads in the Python Forum
- Previous Thread: Button Binding Question:
- Next Thread: Center a Tkinter Window
Views: 2931 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Python
advanced anydbm app bash beginner bits calling chmod cmd code data dictionary directory dynamic edit examples excel external feet file float format ftp function gui halp homework http i/o images import info input ip itunes java keycontrol line linux list lists loan loop maintain millimeter mouse newb number numbers output panel parsing path port prime print program programming projects push py-mailer py2exe pygame pyqt python queue random recursion recursive scrolledtext smtp split ssh string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode update urllib urllib2 variable variables ventrilo web webservice whileloop windows wxpython xlib






