Solution
Section 13, Lecture 161
Solution
1. def get_selected_row(event):
2. try:
3. global selected_tuple
4. index=list1.curselection()[0]
5. selected_tuple=list1.get(index)
6. e1.delete(0,END)
7. e1.insert(END,selected_tuple[1])
8. e2.delete(0,END)
9. e2.insert(END,selected_tuple[2])
10. e3.delete(0,END)
11. e3.insert(END,selected_tuple[3])
12. e4.delete(0,END)
13. e4.insert(END,selected_tuple[4])
14. except IndexError:
15. pass
Explanation
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
indented block under try . If there is an IndexError none of the lines under try will
be executed. Instead the line under except will be executed which is pass .
The pass stetement means do nothing. So the function will do nothing when there's
an empty listbox.
Browse Q&A
Continue