I want to be able to use the button “Add” multiple times but at the moment it only works once. I only have pysimplegui imported. Heres part of the code because looks like i have a limit of characters or something. Hope I don’t have to wait 3 days again…
[sg.Text("Index")], [sg.Listbox(load_list(), size=(17,16), key='-LISTBOX-')], [sg.Button("Add", key='-ADD-'), sg.Button("Delete", key='-DELETE-')] ] window = sg.Window("Index", layout) add_window = sg.Window('Add a name', adicionar_layout) # Event Loop while True: event, values = window.read() if event is None: break if event == '-ADD-': add, input = add_window.read() if add == '-SUBMIT-': new_name = input.get('-INPUTADD-') with open('saved_names.txt', 'a') as f: f.write('n' + new_name) window.Element('-LISTBOX-').Update((load_list())) add_window.close() print(load_list()) window.close()
Advertisement
Answer
Your add_window
closed after first-time event -ADD-
, so it doesn’t work after it.
Add one new popup to get new name and return input value to main window.
import PySimpleGUI as sg def popup_get_new_name(): layout = [ [sg.Text("New Name"), sg.Input(key='INPUT')], [sg.Button("Submit"), sg.Button('Cancel')], ] window = sg.Window("New Name", layout, modal=True) while True: event, values = window.read() if event in (sg.WINDOW_CLOSED, "Cancel"): new_name = None elif event == "Submit": new_name = values['INPUT'] break window.close() return new_name sg.theme("DarkBlue3") sg.set_options(font=("Courier New", 12)) layout = [ [sg.Button("Add")], [sg.StatusBar("", size=(40, 1), key='STATUS')], ] window = sg.Window('Add New Name', layout, finalize=True) while True: event, values = window.read() if event == sg.WINDOW_CLOSED: break elif event == 'Add': new_name = popup_get_new_name() if new_name is not None: window['STATUS'].update(value=new_name) window.close()