Tk Listbox - Selected Item ?

Discussion in 'Python' started by Peter Moscatt, Apr 14, 2005.

  1. 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
    Peter Moscatt, Apr 14, 2005
    #1
    1. Advertising

  2. 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
    Martin Franklin, Apr 14, 2005
    #2
    1. Advertising

  3. 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
    Peter Moscatt, Apr 15, 2005
    #3
  4. 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.
    Martin Franklin, Apr 15, 2005
    #4
  5. 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
    Peter Moscatt, Apr 15, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig Buchanan

    Get the selected item from a listbox

    Craig Buchanan, Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,856
    Craig Buchanan
    Jun 26, 2003
  2. rohith
    Replies:
    3
    Views:
    5,968
    rohith
    Aug 29, 2003
  3. Iain
    Replies:
    3
    Views:
    914
  4. K B
    Replies:
    2
    Views:
    653
  5. mldardy
    Replies:
    0
    Views:
    950
    mldardy
    Sep 28, 2010
Loading...

Share This Page