3

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

Salvatore
  • 10,815
  • 4
  • 31
  • 69
  • 1
    You can generate a click of the button as if the user clicked on it by calling its `click()` method. From [docs](https://pysimplegui.readthedocs.io/en/latest/call%20reference/). – martineau Mar 01 '22 at 01:00

1 Answers1

4

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)
Salvatore
  • 10,815
  • 4
  • 31
  • 69
  • 5
    Call method `write_event_value(key, value)` of Window to fire off an event. – Jason Yang May 14 '22 at 04:06
  • For those confused as I was with how to use [`write_event_value()`](https://www.pysimplegui.org/en/latest/call%20reference/#write_event_value). `key` needs to be the key of the element you would like to trigger. `value` needs to be a possible value for that element. Ex. if `key` is to a combo box, `value` must be a possible selection of that combo box. I think this could be made more clear in the docs. @JasonYang – Matt Popovich Jun 03 '23 at 06:43