Skip to content
Advertisement

How can I programmatically trigger an event with PySimpleGUI?

For example, the "Show" event in the example below is tied to clicking the "Show" button. Is there a way to programmatically fire off the "Show" event without actually clicking the button? The goal is to automate clicking a series of buttons and filling text boxes by just clicking one other button instead, like a browser autofill.

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

Advertisement

Answer

From martineau’s comment above:

You can generate a click of the button as if the user clicked on it by calling its click() method. See the docs.

Aditionally, you can fire a specific event with:

write_event_value(key, value)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement