I'm using a function that allows the user to select an item from a list and store the index into a variable. I would like to use that variable outside the function but it's not working.
Here's what I have so far. (FYI: mylist section code is not shown)
class FirstWindow(Frame):
selection_index = '0'
def __init__(self, parent, controller):
Frame.__init__(self, parent)
num_lines_lights = 4
scrollbar = Scrollbar(self)
#scrollbar.pack(side=RIGHT, fill=Y)
scrollbar.place(x=55,y=200)
#scrollbar.place(x=25, y=50)
mylist = Listbox(self, yscrollcommand = scrollbar.set)
for line in range(num_lines_lights):
mylist.insert(END, "Light " + str(line))
mylist.pack(side = LEFT, fill = BOTH)
mylist.place(x=70,y=200)
scrollbar.config(command = mylist.yview)
def select_item(event):
global selection_index
widget = event.widget
selection_index = (widget.curselection()[0])
value = widget.get(index)
print("Inside function %s " % selection_index) #works fine
mylist.bind('<<ListboxSelect>>', select_item)
print("This is return %s " % selection_index) #NOT WORKING
And here's the error I receive:
print("This is return %s " % selection_index)
NameError: name 'selection_index' is not defined