Tkinter, curselection()[0], IndexError: tuple index out of range Tkinter, curselection()[0], IndexError: tuple index out of range tkinter tkinter

Tkinter, curselection()[0], IndexError: tuple index out of range


Your method is being triggered when there is nothing selected. The easiest fix is to simply check if the tuple is empty:

def get_selected_row(event):    global selected_tuple    index=list1.curselection()    if index: # if the tuple is not empty        selected_tuple=list1.get(index[0])        e1.delete(0,END)        e1.insert(END,selected_tuple[1])        e2.delete(0,END)        e2.insert(END,selected_tuple[2])        e3.delete(0,END)        e3.insert(END,selected_tuple[3])        e4.delete(0,END)        e4.insert(END,selected_tuple[4])

A more proper fix is to find out when it's being triggered like that and prevent it.


it happens when you click into the list1 while it is emptythis is the line that triggers it

list1.bind('<<ListboxSelect>>',get_selected_row)

the solution mentioned above is suitable since you don't seem to have any other code manipulating this eventhowever, i would word the solution a bit differently, to be consistent with your code naming conventions:

def get_selected_row(event):    global selected_tuple    if list1.curselection():        index=list1.curselection()[0]        selected_tuple=list1.get(index)        e1.delete(0,END)        e1.insert(END,selected_tuple[1])        e2.delete(0,END)        e2.insert(END,selected_tuple[2])        e3.delete(0,END)        e3.insert(END,selected_tuple[3])        e4.delete(0,END)        e4.insert(END,selected_tuple[4])


Since the listbox is empty, then list1.curselection()will be an empty list with no items. Trying to access the first item of that list with [0] in line 3 will throw an error since there is no first item in the list.

def get_selected_row(event):    try:        global selected_tuple        index=list1.curselection()        selected_tuple=list1.get(index[0])        e1.delete(0,END)        e1.insert(END,selected_tuple[1])        e2.delete(0,END)        e2.insert(END,selected_tuple[2])        e3.delete(0,END)        e3.insert(END,selected_tuple[3])        e4.delete(0,END)        e4.insert(END,selected_tuple[4])    except IndexError:        pass

When the get_selected_row function is called, Python will try to execute the indentedblock under try. If there is an IndexErrornone of the lines under try will be executed. Instead the line under except will be executed which is pass. Thepass stetementmeans do nothing. So the function will do nothing when there's an empty listbox.