代码拉取完成,页面将自动刷新
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
Copyright 2023 PySimpleSoft, Inc. and/or its licensors. All rights reserved.
Redistribution, modification, or any other use of PySimpleGUI or any portion thereof is subject to the terms of the PySimpleGUI License Agreement available at https://eula.pysimplegui.com.
You may not redistribute, modify or otherwise use PySimpleGUI or its contents except pursuant to the PySimpleGUI License Agreement.
"""
QT_ENTER_KEY1 = 'special 16777220'
QT_ENTER_KEY2 = 'special 16777221'
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-')], ]
window = sg.Window('My new window', layout,
return_keyboard_events=True)
while True: # Event Loop
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event in ('\r', QT_ENTER_KEY1, QT_ENTER_KEY2): # Check for ENTER key
# go find element with Focus
elem = window.find_element_with_focus()
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
elif event == '-1-':
print('Button 1 clicked')
elif event == '-2-':
print('Button 2 clicked')
elif event == '-3-':
print('Button 3 clicked')
window.close()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。