如何使用 PySimpleGUI 以编程方式触发事件?

Mat*_*one 4 python pysimplegui

例如,"Show"下例中的事件与单击按钮相关"Show""Show"有没有一种方法可以在不实际单击按钮的情况下以编程方式触发事件?目标是通过单击另一个按钮来自动单击一系列按钮并填充文本框,就像浏览器自动填充一样。

import PySimpleGUI as sg

sg.theme("BluePurple")

layout = [
    [sg.Text("Your typed chars appear here:"), sg.Text(size=(15, 1), key="-OUTPUT-")],
    [sg.Input(key="-IN-")],
    [sg.Button("Show"), sg.Button("Exit")],
]

window = sg.Window("Pattern 2B", layout)

while True:  # Event Loop
    event, values = window.read()
    print(event, values)
    if event == sg.WIN_CLOSED or event == "Exit":
        break
    if event == "Show":
        # Update the "output" text element to be the value of "input" element
        window["-OUTPUT-"].update(values["-IN-"])

window.close()

Run Code Online (Sandbox Code Playgroud)

Mat*_*one 5

来自上面马蒂诺的评论:

您可以通过调用按钮的方法来生成按钮的单击,就像用户单击它一样click()。请参阅文档

此外,您可以通过以下方式触发特定事件:

write_event_value(key, value)
Run Code Online (Sandbox Code Playgroud)

  • 调用 Window 的方法 write_event_value(key, value) 来触发事件。 (5认同)