I am trying understand how to return a list from a function that is used when the user uses the mouse key to move an item in the box on the left to the box on the right. I am trying to capture the items in the box in the right as a list but need that list outside of the function.
So I am trying to return the list (current_list
) from the select_events
function. It's capturing the list in the function but I need to use it outside. I am struggling to understand how to use the return
to do this
from tkinter import *
from tkinter import ttk
my_window = Tk()
my_frame_in = Frame(my_window)
my_frame_in.grid(row=0, column=0)
my_frame_out = Frame(my_window)
my_frame_out.grid(row=0, column=1)
listbox_events = Listbox(my_frame_in, height='5')
listbox_events.grid(row=0, column=0, padx=10, pady=10)
listbox_events_filtered = Listbox(my_frame_out, height='5')
listbox_events_filtered.grid(row=0, column=2, padx=(0, 10), pady=10)
my_instructions = Label(my_window, text='Use arrow keys to move selected items')
my_instructions.grid(row=1, column=0, columnspan=3, pady=(0, 10))
my_list_events = ['A', 'B', 'C', 'D']
for item in my_list_events:
listbox_events.insert(END, item)
global current_list
def select_events(event=None):
# current_list = []
listbox_events_filtered.insert(END, listbox_events.get(ANCHOR))
listbox_events.delete(ANCHOR)
current_list = list(listbox_events_filtered.get(0, END))
print(current_list)
return current_list
print(current_list)
def deselect_events(event=None):
# current_list = []
listbox_events.insert(END, listbox_events_filtered.get(ANCHOR))
listbox_events_filtered.delete(ANCHOR)
current_list = list(listbox_events_filtered.get(0, END))
listbox_events.bind('<Right>', select_events)
listbox_events_filtered.bind('<Left>', deselect_events)
mainloop()