简体   繁体   English

如何在 PySimpleGUI 中的 InputText 中按下 Enter 时引发事件

[英]How to raise an event when Enter is pressed into an InputText in PySimpleGUI

i'm new with Python & PySimpleGUI and i don't know how to force my application to handle the event key;我是 Python 和 PySimpleGUI 的新手,我不知道如何强制我的应用程序处理事件键; 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.我还没有找到任何类似于 Button 类中使用的“bind_return_key=True”的 InputText 参数。 How can i do it?我该怎么做? Thanks谢谢

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.使用方法bind("<Return>", key_modifier)sg.Input窗口完成后,它会产生一个事件sg.Input().Key + key_modifier在此元素聚焦和输入键按下。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM