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:
JavaScript
x
27
27
1
# Text field
2
sg.InputText(
3
key=key,
4
default_text='',
5
enable_events=True,
6
),
7
8
9
10
11
12
window = self.graphic_interface.window
13
14
while self.is_running:
15
self.event, self.values = window.read(timeout=0)
16
17
18
19
20
if event in INSERT_SN_KEYS:
21
key = event
22
23
if values[key]is not None:
24
# Accept only numbers
25
if values[key][-1] not in '0123456789':
26
window[key].update(values[key][:-1])
27
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.
JavaScript
1
21
21
1
import PySimpleGUI as sg
2
3
sg.theme("DarkBlue3")
4
sg.set_options(font=("Courier New", 16))
5
6
layout = [
7
[sg.Input("Enter to generate an event", key='Input1')],
8
[sg.Input("Enter not to generate an event", key='Input2')],
9
]
10
window = sg.Window('Title', layout, finalize=True)
11
window['Input1'].bind("<Return>", "_Enter")
12
13
while True:
14
event, values = window.read()
15
if event == sg.WINDOW_CLOSED:
16
break
17
elif event == "Input1" + "_Enter":
18
print(event)
19
20
window.close()
21