7
import PySimpleGUI as sg
import os

    layout = [[sg.Text('Velg mappe som skal tas backup av og hvor du vil plassere backupen')],
              [sg.Text('Source folder', size=(15, 1)), sg.InputText(), sg.FolderBrowse()],
              [sg.Text('Backup destination ', size=(15, 1)), sg.InputText(), sg.FolderBrowse()],
              [sg.Text('Made by Henrik og Thomas™')],
              [sg.Submit(), sg.Cancel()]]
    window = sg.Window('Backup Runner v2.1')

    event, values = window.Layout(layout).Read()

How can I call a function when I press the submit button? or any other button?

Niko Föhr
  • 28,336
  • 10
  • 93
  • 96
Kickdak
  • 197
  • 1
  • 3
  • 15
  • I just added to my answer that you don't have to add an event loop. You can put the "if" statements right after your last line of code, the Read call. Thanks for marking the question as answered. – Mike from PSG Apr 05 '19 at 15:20

1 Answers1

9

The PySimpleGUI documentation discusses how to do this in the section on events / callbacks https://pysimplegui.readthedocs.io/#the-event-loop-callback-functions

It is not lot the other Python GUI frameworks that use callbacks to signal button presses. Instead all button presses are returned as "events" coming back from a Read call.

To achieve a similar result, you check the event and make the function call yourself.

import PySimpleGUI as sg

def func(message):
    print(message)

layout = [[sg.Button('1'), sg.Button('2'), sg.Exit()] ]

window = sg.Window('ORIGINAL').Layout(layout)

while True:             # Event Loop
    event, values = window.Read()
    if event in (None, 'Exit'):
        break
    if event == '1':
        func('Pressed button 1')
    elif event == '2':
        func('Pressed button 2')
window.Close()

To see this code run online, you can run it here using the web version: https://repl.it/@PySimpleGUI/Call-Func-When-Button-Pressed

Added 4/5/2019 I should have also stated in my answer that you could add the event checks to right after your call to Read. You don't have to use an Event Loop as I showed. It could look like this:

event, values = window.Layout(layout).Read()   # from OP's existing code
if event == '1':
    func('Pressed button 1')
elif event == '2':
    func('Pressed button 2')

[ Edit Nov 2020 ] - Callable keys

This isn't a new capability, just didn't mention it in the answer previously.

You can set keys to be functions and then call them when the event is generated. Here is an example that uses a few ways of doing this.

import PySimpleGUI as sg

def func(message='Default message'):
    print(message)

layout = [[sg.Button('1', key=lambda: func('Button 1 pressed')), 
           sg.Button('2', key=func), 
           sg.Button('3'), 
           sg.Exit()]]

window = sg.Window('Window Title', layout)

while True:             # Event Loop
    event, values = window.read()
    if event in (None, 'Exit'):
        break
    if callable(event):
        event()
    elif event == '3':
        func('Button 3 pressed')

window.close()
Mike from PSG
  • 5,312
  • 21
  • 39