def get_selected_row(event): try: global selected_tuple 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]) except IndexError: pass
As you can see the error was fixed by simply implementing a try
and except
block. 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.