Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I select from a Listbox without errors?

I want to select from a Listbox and use the value selected to retrieve other data which will also be inserted to another Listbox for selection. When selecting the first Listbox, it works successfully without any error. The second Listbox also gets updated with new list on selecting the first one. The problem comes when selecting the second Listbox, everything expected works fine except that there is an error exception that comes with it.

from tkinter import *
root= Tk()
books = ['Genesis', 'Exodus', 'Leviticus']
chapter = [1,2,3,4,5]
biblelist = Listbox(root)
biblelist.pack(side=LEFT)
biblelist.insert(END, 0)

chapter = Listbox(root)
chapters.pack(side=LEFT)

def get_selected(e):
    global book_selected
    index = int(biblelist.curselection()[0])
    value=biblelist.get(index)
    book_selected = '%s' %(value)
    if book_selected == books[0]
        for x in chapter:
            chapters.insert(END, x)


def get_selected_chapter(event):
    ind = int(chapters.curselection()[0])
    val=chapters.get(ind)
    chapter_selected = '%s' %(val)
    print(book_selected + chapter_selected)

biblelist.bind('<<ListboxSelect>>',get_selected)
chapters.bind('<<ListboxSelect>>', get_selected_chapter)
root.mainloop()

The code real gives me what I want but I am not comfortable with the error.
This is the error:
File "C:\Users\a\Desktop\emma\PROJECT\index.py", line 685, in get_selected index = int(biblelist.curselection()[0]) ~~~~~~~~~~~~~~~~~~~~~~~~^^^ IndexError: tuple index out of range

I tried all around to remove the error but still failed. I really need help here. I am also new to programming in python.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The error you’re encountering (IndexError: tuple index out of range) occurs because you’re trying to access an index of an empty tuple. This happens when curselection() returns an empty tuple because no item in the Listbox is selected. To handle this, you should first check if there’s a selection before trying to access its index.

For example.
def get_selected(e):
global book_selected
if biblelist.curselection(): # Add this port for error handling
index = int(biblelist.curselection()[0])

def get_selected_chapter(event):
if chapters.curselection(): # Error handling
ind = int(chapters.curselection()[0])

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading