En Tue, 18 Dec 2007 00:46:27 -0300, Sam Garson <[EMAIL PROTECTED]> 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 -- http://mail.python.org/mailman/listinfo/python-list