I'm trying to write a program that returns the index of a selected Listbox item to the function where the bind function was called.
The below program simply prints the index of the selected item inside the get_index function once the item is clicked; however, I want the index to be returned to the main function rather than being contained in the get_index function. Is what I'm trying to accomplish even possible?
import tkinter as tk
def get_index(event):
selection = event.widget.curselection()
if selection:
index = selection[0]
print(index) # Want to return here instead of print
else:
pass
def main():
root = tk.Tk()
root.geometry("300x150")
my_list = tk.Listbox(root)
my_list.pack(pady=15)
options = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
]
for option in options:
my_list.insert("end", option)
my_list.bind("<<ListboxSelect>>", get_index)
root.mainloop()
if __name__ == "__main__":
main()