Home > Software design >  How to bind an interface to functions in PySimpleGUI
How to bind an interface to functions in PySimpleGUI

Time:10-03

I have a next files: https://github.com/Greenboyisyourdream/PasswordGenerator

How can i do to make the "generate" button execute the function on line 12 in the app.py

(The function on line 12 in the app.py):

elif button == "Generate!":
    screen.format()
    screen = str(*[random.choice(list(symbols)) for _ in range(lenght)])
    window.FindElement("output").Update(screen)

CodePudding user response:

In layout, you define key of gui.InputText as 'output', at the same time you also define key of gui.Button as same 'output'.

Using unique key for all elements in same window.

key of gui.Button here will be automatically changed to 'output0'.

layout = [
...
    [gui.InputText(screen, size=(40, 1), font=("Times New Roman", 10), background_color="white", text_color="black", key="output"),
     gui.Button(button_text="Generate!", size=(10, 1), button_color="red", key="output")]

]

The event generated when button clicked is the key of gui.Button, not button_text of element if option key or k specified.

The most easy solution to get right event 'Generated!' is to remove option key='output' from gui.Button, or set option key='Generated!' in gui.Button.

layout = [
...
    [gui.InputText(screen, size=(40, 1), font=("Times New Roman", 10), background_color="white", text_color="black", key="output"),
     gui.Button(button_text="Generate!", size=(10, 1), button_color="red")]

]
  • Related