Zack
Zack

Reputation: 31

PySimpleGui: How to add values from one listbox to another

So I've been looking for an answer to this for quite a while. Apologies if I'm just overlooking something here.

I want to create two listboxes where you can move the values from one listbox into another listbox, creating your own order for those values. This is because I'm trying to make a GUI for an API, and the order of the values will ultimately determine the order of dimension/metric headers, which is extremely important.

This was the approach I tried to take to achieve this, however, it appears the TEST object just overwrites its list everytime the "Add" button is pressed, whereas I want it to append:

import PySimpleGUI as sg

list1 = ["a", "b", "c"]


layout = [[sg.Text("Demo")],      
                 [sg.Listbox(values=list1, size=(30,6), enable_events=True, key="-LIST-"), sg.Button("Add", enable_events=True, key="-BUTTON-"), sg.Listbox(values=[], size=(30,6), key="-LIST2-")], 
                 [sg.Cancel("Exit")]]  

window = sg.Window("Beta", layout=layout, background_color="#272533", size=(650, 450))

while True:
    event, values = window.read()
    if event == "Exit" or event == sg.WIN_CLOSED:
        break
    
    if event == "-BUTTON-":
        TEST = []
        TEST.append(values["-LIST-"])
        window["-LIST2-"].update(TEST)
        print(TEST)

window.close()

Upvotes: 1

Views: 3772

Answers (1)

Zack
Zack

Reputation: 31

Thank you Jason, I managed to set up two listboxes such that you can move items from one box to another and thereby create whatever order of items you want by using get_indexes().

Basically I started with an empty list, then used get_indexes() to find the index of the value a user determines in the initial list. I then append that value onto the empty list using the pop function so it is deleted from the initial list. Then I had to update both windows so the indexes would update for when the user moves the next value.

Here's the code I used to get it to work. It's probably not the most beautiful or efficient (I'm quite new to python) but it does work! If there are any suggestions for how to make it more succinct they would be appreciated!

import PySimpleGUI as sg

list1 = ['a', 'b', 'c']
list2 = []


layout = [[sg.Text("Demo")],      
                 [sg.Listbox(values=list1, size=(30,6), enable_events=True, key="-LIST-"), sg.Button("Add", enable_events=True, key="-BUTTON-"), sg.Button("Remove", enable_events=True, key="-BUTTON2-"), sg.Listbox(values=list2, size=(30,6), key="-LIST2-")], 
                 [sg.Cancel("Exit")]]  

window = sg.Window("Beta", layout=layout, background_color="#272533", size=(650, 450))

while True:
    event, values = window.read()
    if event == "Exit" or event == sg.WIN_CLOSED:
        break
    
    if event == "-BUTTON-":
        INDEX = int(''.join(map(str, window["-LIST-"].get_indexes())))
        list2.append(list1.pop(INDEX))
        window["-LIST2-"].update(list2)
        window["-LIST-"].update(list1)

    if event == "-BUTTON2-":
        INDEX = int(''.join(map(str, window["-LIST2-"].get_indexes())))
        list1.append(list2.pop(INDEX))
        window["-LIST2-"].update(list2)
        window["-LIST-"].update(list1)

window.close()

Upvotes: 2

Related Questions