FAQ
Hi again.

I am trying to get the text of the selection in a list box and put into a
variable, which is put into a label. i.e., whatever is selected will show in
the label.

However, because at the beginning there is nothing in the listbox (and
therefore no selection), the tuple returned by curselection() is (),
therefore when i try and get() from the linenumber in this tuple, the index
is "out of range"

here is the section of the program:

box = Listbox(root, bg = '#ebe9ed', relief = 'groove', height = 15)
box.grid(row = 2, columnspan = 2, sticky = W+E, padx = 3)
box.bind("<Double-Button-1>", DeleteCurrent)

v = StringVar()
number = box.curselection()
v.set = (box.get(int(number[0])))
print number

current = Label(root, textvariable = v)
current.grid(row = 3, columnspan = 2, sticky = W+E, padx = 3)

and here is the error:

Traceback (most recent call last):
File "C:\My Documents\My Python\Notes.py", line 27, in <module>
v.set = (box.get(int(number[0])))
IndexError: tuple index out of range

How can i get it to only take the variable when i have selected something,
or any other solution!

Thanks,

Sam

--
I intend to live forever - so far, so good.

SaM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-list/attachments/20071218/3793ee3f/attachment.htm

Search Discussions

  • Gabriel Genellina at Dec 18, 2007 at 4:07 am
    En Tue, 18 Dec 2007 00:46:27 -0300, Sam Garson <peanut.sam at googlemail.com>
    escribi?:
    I am trying to get the text of the selection in a list box and put into a
    variable, which is put into a label. i.e., whatever is selected will
    show in
    the label.

    However, because at the beginning there is nothing in the listbox (and
    therefore no selection), the tuple returned by curselection() is (),
    therefore when i try and get() from the linenumber in this tuple, the
    index
    is "out of range"

    here is the section of the program:

    box = Listbox(root, bg = '#ebe9ed', relief = 'groove', height = 15)
    box.grid(row = 2, columnspan = 2, sticky = W+E, padx = 3)
    box.bind("<Double-Button-1>", DeleteCurrent)

    v = StringVar()
    number = box.curselection()
    v.set = (box.get(int(number[0])))
    print number

    current = Label(root, textvariable = v)
    current.grid(row = 3, columnspan = 2, sticky = W+E, padx = 3)

    and here is the error:

    Traceback (most recent call last):
    File "C:\My Documents\My Python\Notes.py", line 27, in <module>
    v.set = (box.get(int(number[0])))
    IndexError: tuple index out of range

    How can i get it to only take the variable when i have selected
    something,
    or any other solution!
    Just avoid using number[0] when number is empty. Empty tuples (and empty
    lists and dictionaries) evaluate to False in a boolean context, so you can
    check this way:

    v = StringVar()
    number = box.curselection()
    if number:
    v.set = (box.get(int(number[0])))
    print number
    ...

    --
    Gabriel Genellina

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedDec 18, '07 at 3:46a
activeDec 18, '07 at 4:07a
posts2
users2
websitepython.org

2 users in discussion

Gabriel Genellina: 1 post Sam Garson: 1 post

People

Translate

site design / logo © 2022 Grokbase