首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PySimpleGUI中的热键

PySimpleGUI中的热键
EN

Stack Overflow用户
提问于 2022-08-20 20:07:18
回答 1查看 507关注 0票数 1

我想用PySimpleGUI编写一个完全可以通过键盘使用的图形用户界面。基于以下示例代码:

代码语言:javascript
复制
import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button(button_text="OK")]]

window = sg.Window("Demo", layout)

while True:
    event, values = window.read()
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()

如何添加热键,可以使用Alt+O按下OK-Button?OK-Button上的O应该加下划线:

EN

回答 1

Stack Overflow用户

发布于 2022-08-20 20:27:11

派生自:https://github.com/PySimpleGUI/PySimpleGUI/issues/4122的极简工作示例

代码语言:javascript
复制
import PySimpleGUI as sg

layout = [
    [sg.Button("ok", size=(10, 2), key='button1'),
     sg.Button("exit", size=(10, 2), key='button2')],
]
window = sg.Window('Hotkeys', layout, use_default_focus=False, finalize=True)
button1, button2 = window['button1'], window['button2']

window.bind("<Alt_L><o>", "ALT-o")
window.bind("<Alt_L><x>", "ALT-x")

button1.Widget.configure(underline=0, takefocus=0)
button2.Widget.configure(underline=1, takefocus=0)

while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event in ("button1", "ALT-o"):
        print('OK')
    elif event in ("button2", "ALT-x"):
        break

window.close()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73429807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档