Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 12:59 AM
Peter Moscatt
Guest
 
Posts: n/a
Default 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

  #2  
Old July 19th, 2005, 12:59 AM
Martin Franklin
Guest
 
Posts: n/a
Default Re: Tk Listbox - Selected Item ?

Peter Moscatt wrote:[color=blue]
> 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
>[/color]


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


  #3  
Old July 19th, 2005, 01:01 AM
Peter Moscatt
Guest
 
Posts: n/a
Default Re: Tk Listbox - Selected Item ?

Martin Franklin wrote:
[color=blue]
> Peter Moscatt wrote:[color=green]
>> 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
>>[/color]
>
>
> 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[/color]

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:
[color=blue]
> | 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).[/color]



Pete



  #4  
Old July 19th, 2005, 01:02 AM
Martin Franklin
Guest
 
Posts: n/a
Default Re: Tk Listbox - Selected Item ?

Peter Moscatt wrote:[color=blue]
> Martin Franklin wrote:
>
>[color=green]
>>Peter Moscatt wrote:
>>[color=darkred]
>>>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
>>>[/color]
>>
>>
>>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[/color]
>
>
> 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.
>[/color]

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.

  #5  
Old July 19th, 2005, 01:02 AM
Peter Moscatt
Guest
 
Posts: n/a
Default Re: Tk Listbox - Selected Item ?

Martin Franklin wrote:
[color=blue]
> Peter Moscatt wrote:[color=green]
>> Martin Franklin wrote:
>>
>>[color=darkred]
>>>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[/color]
>>
>>
>> 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.
>>[/color]
>
> 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.[/color]

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

 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles