-1

I'm trying to import a csv file into a listbox, then when I double click one of the entries I want it to be added to a second listbox on screen, so that I can set the entries in the second listbox to another variable that will be called later in the program.

Windows 10 Python 3.6 most recent pysimplegui

Here's what my code currently looks like:

def open_listbox(list_a) #list_a is a user defined csv from previous function
    list_b = []     #initializing list_b
    list_b.append([])

    layout = [[sg.Listbox(values=list_a, key='-list_a-', enable_events=True, bind_return_key=True, size=(60, 20)), sg.Listbox(values=list_b, key='-list_b-', enable_events=True, bind_return_key=True, size=(60, 20))],
    [sg.Ok(), sg.Cancel()]]

    window = sg.Window('test', layout)
    while True:
        event, values = window.read()
        if event == sg.WINDOW_CLOSED or event == 'Cancel':
            break
        elif event == list_a[int()]:
            sg.clipboard_set(list_a[int()].append(list_b)
            print(list_b)
        elif event == 'Ok':
            break
    print(list_b)
    window.close()

When I click on any of the entries nothing seems to happen, and in the terminal when it goes to print list_b right before closing the window it is always an empty list.

Mike Shaw
  • 3
  • 2

1 Answers1

0

It looks something wrong for the way you check the event and append the new item.

Following code for your reference.

import PySimpleGUI as sg

items = [f'Item {i+1:0>2d}' for i in range(10)]

layout = [[sg.Listbox(items, size=(10, 5), key='List1'), sg.Listbox([], size=(10, 5), key='List2')]]
window = sg.Window("Title", layout, finalize=True)
for key in ("List1", "List2"):
    window[key].bind("<Double-Button-1>", "")
selected, lst = set(), []

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    print(event)
    if event == "List1":
        item = values[event][0]
        selected.add(item)
        lst = list(selected)
        lst.sort()
        index = lst.index(item)
        window['List2'].update(lst, set_to_index=index, scroll_to_index=index)
    elif event == "List2":
        if values[event]:
            item = values[event][0]
            index = lst.index(item)
            lst.remove(item)
            selected.remove(item)
            window['List2'].update(lst)
            if index > 0:
                window['List1'].update(scroll_to_index=index-1)
print(lst)
window.close()
Jason Yang
  • 11,284
  • 2
  • 9
  • 23