473,322 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Tk Listbox - Selected Item ?

I am having trouble understanding the methods for the Listbox from Tk.

If I was to select at item in the list using a mouse click (have already
created the bind event) - what method returns the text of the selected
item ?

Pete

Jul 18 '05 #1
4 22570
Peter Moscatt wrote:
I am having trouble understanding the methods for the Listbox from Tk.

If I was to select at item in the list using a mouse click (have already
created the bind event) - what method returns the text of the selected
item ?

Pete

Pete,

pydoc Tkinter.Listbox

<snip>

| curselection(self)
| Return list of indices of currently selected item.
|
| delete(self, first, last=None)
| Delete items from FIRST to LAST (not included).
|
| get(self, first, last=None)
| Get list of items from FIRST to LAST (not included).

So to get the value of the selected item:

lb.get(lb.curselection()[0])
provided the listbox is in single selection mode or only one item is
selected

Martin
Jul 18 '05 #2
Martin Franklin wrote:
Peter Moscatt wrote:
I am having trouble understanding the methods for the Listbox from Tk.

If I was to select at item in the list using a mouse click (have already
created the bind event) - what method returns the text of the selected
item ?

Pete
Pete,

pydoc Tkinter.Listbox

<snip>

| curselection(self)
| Return list of indices of currently selected item.
|
| delete(self, first, last=None)
| Delete items from FIRST to LAST (not included).
|
| get(self, first, last=None)
| Get list of items from FIRST to LAST (not included).

So to get the value of the selected item:

lb.get(lb.curselection()[0])
provided the listbox is in single selection mode or only one item is
selected

Martin


Thanks Martin,

I used the:
lb.get(lb.curselection()[0])

ant this works to a point. When I select the item in the listbox the system
generates an error:

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.3/lib-tk/Tkinter.py", line 1345, in __call__
return self.func(*args)
File "/home/linux/programming/dxcluster/servers.py", line 37, in sel
items = self.listbox.get(self.listbox.curselection()[0])
IndexError: tuple index out of range
Then if I select a second time directly after the error message I get my
desired result.

Am I to you the code you quoted in your original message:
| curselection(self)
| Return list of indices of currently selected item.
|
| delete(self, first, last=None)
| Delete items from FIRST to LAST (not included).
|
| get(self, first, last=None)
| Get list of items from FIRST to LAST (not included).


Pete

Jul 19 '05 #3
Peter Moscatt wrote:
Martin Franklin wrote:

Peter Moscatt wrote:
I am having trouble understanding the methods for the Listbox from Tk.

If I was to select at item in the list using a mouse click (have already
created the bind event) - what method returns the text of the selected
item ?

Pete

Pete,

pydoc Tkinter.Listbox

<snip>

| curselection(self)
| Return list of indices of currently selected item.
|
| delete(self, first, last=None)
| Delete items from FIRST to LAST (not included).
|
| get(self, first, last=None)
| Get list of items from FIRST to LAST (not included).

So to get the value of the selected item:

lb.get(lb.curselection()[0])
provided the listbox is in single selection mode or only one item is
selected

Martin

Thanks Martin,

I used the:
lb.get(lb.curselection()[0])

ant this works to a point. When I select the item in the listbox the system
generates an error:

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.3/lib-tk/Tkinter.py", line 1345, in __call__
return self.func(*args)
File "/home/linux/programming/dxcluster/servers.py", line 37, in sel
items = self.listbox.get(self.listbox.curselection()[0])
IndexError: tuple index out of range
Then if I select a second time directly after the error message I get my
desired result.


Pete,

Sounds like you are using the wrong kind of bind event. That is the
first time you select an item the callback for the bind command is
called *before* the selection is made. Can you post the code you have
showing the bind method and callback?

This code shows three different bind methods, the first produces the
same exception that you got...
from Tkinter import *
root = Tk()
lb = Listbox(root)
lb.insert(0, "one")
lb.insert(0, "two")
lb.insert(0, "three")
lb.pack()

def callback(*event):
print lb.get(lb.curselection()[0])

## BAD
#~ lb.bind("<1>", callback)

## OK
#~ lb.bind("<ButtonRelease-1>", callback)

## Best
lb.bind("<<ListboxSelect>>", callback)

root.mainloop()
The <<ListboxSelect>> is a virtual event I think quick google would tell
you more and the Tk man pages (online) have a complete explanation...

http://www.tcl.tk/man/tcl8.4/TkCmd/listbox.htm
http://www.tcl.tk/man/tcl8.4/TkCmd/listbox.htm#M60
Cheers,
Martin.

Jul 19 '05 #4
Martin Franklin wrote:
Peter Moscatt wrote:
Martin Franklin wrote:

Peter Moscatt wrote:

I am having trouble understanding the methods for the Listbox from Tk.

If I was to select at item in the list using a mouse click (have already
created the bind event) - what method returns the text of the selected
item ?

Pete

Pete,

pydoc Tkinter.Listbox

<snip>

| curselection(self)
| Return list of indices of currently selected item.
|
| delete(self, first, last=None)
| Delete items from FIRST to LAST (not included).
|
| get(self, first, last=None)
| Get list of items from FIRST to LAST (not included).

So to get the value of the selected item:

lb.get(lb.curselection()[0])
provided the listbox is in single selection mode or only one item is
selected

Martin

Thanks Martin,

I used the:
lb.get(lb.curselection()[0])

ant this works to a point. When I select the item in the listbox the
system generates an error:

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.3/lib-tk/Tkinter.py", line 1345, in __call__
return self.func(*args)
File "/home/linux/programming/dxcluster/servers.py", line 37, in sel
items = self.listbox.get(self.listbox.curselection()[0])
IndexError: tuple index out of range
Then if I select a second time directly after the error message I get my
desired result.


Pete,

Sounds like you are using the wrong kind of bind event. That is the
first time you select an item the callback for the bind command is
called *before* the selection is made. Can you post the code you have
showing the bind method and callback?

This code shows three different bind methods, the first produces the
same exception that you got...
from Tkinter import *
root = Tk()
lb = Listbox(root)
lb.insert(0, "one")
lb.insert(0, "two")
lb.insert(0, "three")
lb.pack()

def callback(*event):
print lb.get(lb.curselection()[0])

## BAD
#~ lb.bind("<1>", callback)

## OK
#~ lb.bind("<ButtonRelease-1>", callback)

## Best
lb.bind("<<ListboxSelect>>", callback)

root.mainloop()
The <<ListboxSelect>> is a virtual event I think quick google would tell
you more and the Tk man pages (online) have a complete explanation...

http://www.tcl.tk/man/tcl8.4/TkCmd/listbox.htm
http://www.tcl.tk/man/tcl8.4/TkCmd/listbox.htm#M60
Cheers,
Martin.


Thanks Martin,

You're a legend !!!!!

I fixed the bind and used lb.bind("<<ListboxSelect>>", callback) and away it
went.

Thanks heaps for the help.

Pete

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Filips Benoit | last post by:
Dear All, I have code that selects - find and select - 1 item in a large listbox. This works Ok but most of the time the selected item isn't visible without scrolling down manualy. Is this...
1
by: | last post by:
Hello everybody... How can i unplug selected item in Listbox. Thanks in advance. Paulo
6
by: David De Cotis | last post by:
Hello all, I am trying to go through a ListBox and verify if am item was selected. If an item was selected, I would like to get a handle of the item and simply do a response.write on the selected...
1
by: Karen Grube | last post by:
Hi! I'm using a standard server side ASP.Net listbox control on a web form. The page is basically various shades of green. The listbox itself has a pale green background and forest green text...
2
by: Hitesh | last post by:
I have a listbox and the values get selected (highlighted) from code. I can highlight the corresponding list box items, but they do not show in the listbox I have to scroll through the list box to...
3
by: Mitch | last post by:
Is the following a correct representation of the relationship of the selected index to selected item: INDEX ITEM 0 "item 1" 1 "item 2" 2 "item 3" and so on. I keep...
0
by: scartin | last post by:
Hi, I have a multi-select ListBox that shows 5 rows at a time, but contains possibly a hundred or more items. As I retrieve relevant data from the database, I set the SelectedIndex of the rows...
3
by: peter.mosley | last post by:
I've tried googling for the answer to this problem, without any luck. I'm sure the truth must be out there somewhere! I have a multiselect listbox populated with many items (set by the RowSource...
1
by: Gerardo ARnaez | last post by:
Hi. I am writing a program to help determine coumadin regimens to look at the code: http://sourceforge.net/projects/coumadinregimen/ The issue is that I have a variable that I want the use to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.