2019-06-22 14:59:24 +00:00
|
|
|
import PySimpleGUI as sg
|
|
|
|
"""
|
|
|
|
tkinter and Qt do not "activate" buttons by pressing the ENTER key with the button highlighted / in focus
|
|
|
|
This demo will enable the application to click on a button if the button has focus (is highlighted) and the
|
|
|
|
user presses the ENTER key.
|
|
|
|
NOTE that the SPACE BAR works correctly out of the box with both tkinter and Qt. If a button has focus and
|
|
|
|
you press the space bar, then tkinter and Qt will both consider that a button click. But not so with the ENTER
|
|
|
|
key.
|
|
|
|
|
|
|
|
The solution is for your program to read the keyboard presses and act upon those directly. It's trivial logic
|
|
|
|
in the end:
|
|
|
|
1. Get a key press
|
|
|
|
2. See if the key is the ENTER key
|
|
|
|
3. Find the Element that currently has focus
|
|
|
|
4. Click the Button if the Element with focus is a button
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
QT_ENTER_KEY1 = 'special 16777220'
|
|
|
|
QT_ENTER_KEY2 = 'special 16777221'
|
2019-06-22 14:59:24 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
layout = [[sg.Text('Test of Enter Key use')],
|
|
|
|
[sg.Input(key='-IN-')],
|
|
|
|
[sg.Button('Button 1', key='-1-')],
|
|
|
|
[sg.Button('Button 2', key='-2-')],
|
|
|
|
[sg.Button('Button 3', key='-3-')], ]
|
2019-06-22 14:59:24 +00:00
|
|
|
|
|
|
|
window = sg.Window('My new window', layout,
|
|
|
|
return_keyboard_events=True)
|
|
|
|
while True: # Event Loop
|
2019-10-23 20:10:03 +00:00
|
|
|
event, values = window.read()
|
2020-05-07 10:22:59 +00:00
|
|
|
if event == sg.WIN_CLOSED:
|
2019-06-22 14:59:24 +00:00
|
|
|
break
|
|
|
|
if event in ('\r', QT_ENTER_KEY1, QT_ENTER_KEY2): # Check for ENTER key
|
2019-10-23 20:10:03 +00:00
|
|
|
# go find element with Focus
|
|
|
|
elem = window.find_element_with_focus()
|
2019-06-22 14:59:24 +00:00
|
|
|
if elem is not None and elem.Type == sg.ELEM_TYPE_BUTTON: # if it's a button element, click it
|
|
|
|
elem.Click()
|
|
|
|
# check for buttons that have been clicked
|
2019-10-23 20:10:03 +00:00
|
|
|
elif event == '-1-':
|
2019-06-22 14:59:24 +00:00
|
|
|
print('Button 1 clicked')
|
2019-10-23 20:10:03 +00:00
|
|
|
elif event == '-2-':
|
2019-06-22 14:59:24 +00:00
|
|
|
print('Button 2 clicked')
|
2019-10-23 20:10:03 +00:00
|
|
|
elif event == '-3-':
|
|
|
|
print('Button 3 clicked')
|
|
|
|
|
|
|
|
window.close()
|