0% found this document useful (0 votes)
27 views

160 - Fixing The Bug

This document discusses a bug in a program where clicking an empty list box generates an IndexError. The bug occurs because when the empty list box is clicked, it calls a function that tries to access the first item selected in the list, which doesn't exist since the list is empty. The document prompts the reader to try and fix the bug.

Uploaded by

jd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

160 - Fixing The Bug

This document discusses a bug in a program where clicking an empty list box generates an IndexError. The bug occurs because when the empty list box is clicked, it calls a function that tries to access the first item selected in the list, which doesn't exist since the list is empty. The document prompts the reader to try and fix the bug.

Uploaded by

jd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Fixing the Bug (Practice)

Section 13, Lecture 160


Exercise
If you haven't noticed it already, the program has a bug. When the list box is
empty and the user clicks it, an IndexError is generated in the terminal:

Why that happens?


That happens because when the user clicks the list box this code is executed:

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

That line calls the get_selected_row function:

1. def get_selected_row(event):
2. global selected_tuple
3. index=list1.curselection()[0]
4. selected_tuple=list1.get(index)
5. e1.delete(0,END)
6. e1.insert(END,selected_tuple[1])
7. e2.delete(0,END)
8. e2.insert(END,selected_tuple[2])
9. e3.delete(0,END)
10. e3.insert(END,selected_tuple[3])
11. e4.delete(0,END)
12. 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.

Please try to fix that bug. The next lecture contains the solution.

You might also like