i’m new with Python & PySimpleGUI and i don’t know how to force my application to handle the event key; this is my code:
# Text field sg.InputText( key=key, default_text='', enable_events=True, ), ... window = self.graphic_interface.window while self.is_running: self.event, self.values = window.read(timeout=0) ... if event in INSERT_SN_KEYS: key = event if values[key]is not None: # Accept only numbers if values[key][-1] not in '0123456789': window[key].update(values[key][:-1])
My code can handle any key pressed by user except ; i haven’t found any argument for InputText similar to “bind_return_key=True” used into Button class. How can i do it? Thanks
Advertisement
Answer
Using method bind("<Return>", key_modifier)
of sg.Input
after window finalized, it will generate an event sg.Input().Key + key_modifier
when this element focused and Enter key pressed.
import PySimpleGUI as sg sg.theme("DarkBlue3") sg.set_options(font=("Courier New", 16)) layout = [ [sg.Input("Enter to generate an event", key='Input1')], [sg.Input("Enter not to generate an event", key='Input2')], ] window = sg.Window('Title', layout, finalize=True) window['Input1'].bind("<Return>", "_Enter") while True: event, values = window.read() if event == sg.WINDOW_CLOSED: break elif event == "Input1" + "_Enter": print(event) window.close()