Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Tk Listbox - Selected Item ?

Reply
Thread Tools

Tk Listbox - Selected Item ?

 
 
Peter Moscatt
Guest
Posts: n/a
 
      04-14-2005
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

 
Reply With Quote
 
 
 
 
Martin Franklin
Guest
Posts: n/a
 
      04-14-2005
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


 
Reply With Quote
 
 
 
 
Peter Moscatt
Guest
Posts: n/a
 
      04-15-2005
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



 
Reply With Quote
 
Martin Franklin
Guest
Posts: n/a
 
      04-15-2005
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.

 
Reply With Quote
 
Peter Moscatt
Guest
Posts: n/a
 
      04-15-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help to fill textboxes from a selected item in datagrid-selected index changed. mldardy ASP .Net 0 09-28-2010 02:59 PM
Listbox move multiple selected items to second listbox K B ASP .Net 2 01-08-2007 11:16 AM
DropDownList 2 always returns Selected = 0 for all items - even selected item Iain ASP .Net 3 12-11-2006 11:07 AM
selected item and dropdown list/listbox rohith ASP .Net 3 08-29-2003 04:32 PM
Get the selected item from a listbox Craig Buchanan ASP .Net 0 06-26-2003 12:47 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57