There is no "linking" of buttons to functions, nor callback functions.

To do what you're looking for, calling copy when you get a "Copy Button"event back from a Read.

I urge you to read through the docs to get an understanding how these calls to Button, etc, work. http://www.PySimpleGUI.org

Here's what I think you are looking for your code to do:

import PySimpleGUI as sg
import shutil, errno
src = ""
dest = ""
def copy(src, dest):
    try:
        shutil.copytree(src, dest)
    except OSError as e:
        # If the error was caused because the source wasn't a directory
        if e.errno == errno.ENOTDIR:
            shutil.copy(src, dest)
        else:
            print('Directory not copied. Error: %s' % e)

#Me testing out commands in PSG
layout = [[ sg.Text("Select path from source to destination")],
[sg.Text("Source Folder", size=(15,1)), sg.InputText(src),
sg.FolderBrowse()],
[sg.Text("Destination Folder", size=(15,1)),
sg.InputText(dest), sg.FolderBrowse()],
[sg.Button("Transfer", button_color=("white", "blue"), size=
(6, 1)),sg.Button("Copy", button_color=("white",
"green"), size=(6, 1)),sg.Exit(button_color=("white", "red"),
size=(6, 1))]]

window = sg.Window("Mass File Transfer").Layout(layout)

while True:
    event, values = window.Read()
    print(event, values)
    if event in (None, 'Exit'):
        break

    if event == 'Copy':
        copy(values[0], values[1])
Answer from Mike from PSG on Stack Overflow
🌐
PySimpleGUI
docs.pysimplegui.com › en › latest › documentation › module › elements › button
Button - PySimpleGUI Documentation
A Dummy button is added to your layout by typing sg.DummyButton. These are special purpose buttons that will close a window without generating an event. Dummy buttons are used with Async Windows. These windows are not closed explicitly by your code. Instead, PySimpleGUI closes the window for ...
🌐
TutorialsPoint
tutorialspoint.com › pysimplegui › pysimplegui_button_element.htm
PySimpleGUI - Button Element
The FileBrowse button opens a file dialog from which a single file can be selected. In the following code, the path string of the selected file is displayed in the target Input bow in the same row. import PySimpleGUI as psg layout = [ [psg.Text('Select a file',font=('Arial Bold', 20), expand_x=True, justification='center')], [psg.Input(enable_events=True, key='-IN-',font=('Arial Bold', 12),expand_x=True), psg.FileBrowse()] ] window = psg.Window('FileChooser Demo', layout, size=(715,100)) while True: event, values = window.read() if event == psg.WIN_CLOSED or event == 'Exit': break window.close()
🌐
ProgramCreek
programcreek.com › python › example › 116002 › PySimpleGUI.Button
Python Examples of PySimpleGUI.Button
Try this')], [sg.Text('Here come the good colors as defined by PySimpleGUI')], [sg.Text('Button Colors Using PySimpleGUI.BLUES')], [*[sg.Button('BLUES[{}]\n{}'.format(j, c), button_color=(sg.YELLOWS[0], c), size=s10) for j, c in enumerate(sg.BLUES)]], [sg.Text('_' * 100, size=(65, 1))], [sg.Text('Button Colors Using PySimpleGUI.PURPLES')], [*[sg.Button('PURPLES[{}]\n{}'.format(j, c), button_color=(sg.YELLOWS[0], c), size=s10) for j, c in enumerate(sg.PURPLES)]], [sg.Text('_' * 100, size=(65, 1))], [sg.Text('Button Colors Using PySimpleGUI.GREENS')], [*[sg.Button('GREENS[{}]\n{}'.format(j, c),
🌐
YouTube
youtube.com › coding is amazing
Python PySimpleGUI - Buttons and menu elements Tutorial #02 - YouTube
AboutPressCopyrightContact usCreatorsAdvertiseDevelopersTermsPrivacyPolicy & SafetyHow YouTube worksTest new featuresNFL Sunday Ticket · © 2025 Google LLC
Published   August 21, 2022
Views   7K
🌐
PySimpleGUI
docs.pysimplegui.com › en › latest › documentation › module › elements › button_menu
ButtonMenu - PySimpleGUI Documentation
The ButtonMenu element produces a unique kind of effect. It's a button, that when clicked, shows you a menu. It's like clicking one of the top-level menu items on a MenuBar.
🌐
GitHub
github.com › PySimpleGUI › PySimpleGUI › blob › master › DemoPrograms › Demo_Button_Toggle.py
PySimpleGUI/DemoPrograms/Demo_Button_Toggle.py at master · PySimpleGUI/PySimpleGUI
You may not redistribute, modify or otherwise use PySimpleGUI or its contents except pursuant to the PySimpleGUI License Agreement. ... sg.Button('', image_data=toggle_btn_off, key='-TOGGLE-GRAPHIC-', button_color=(sg.theme_background_color(), sg.theme_background_color()), border_width=0)],
Author   PySimpleGUI
🌐
GitHub
github.com › PySimpleGUI › PySimpleGUI › issues › 3412
[ Help Wanted ] Nice looking button graphics wanted.... · Issue #3412 · PySimpleGUI/PySimpleGUI
September 25, 2020 - Today a new Demo Program was released that shows a number of base64 buttons defined and in use in a program.
Author   PySimpleGUI
Find elsewhere
🌐
GitHub
github.com › PySimpleGUI › PySimpleGUI › issues › 5375
[Question] trying to find help on how to place buttons in different area of a window · Issue #5375 · PySimpleGUI/PySimpleGUI
April 17, 2022 - [x ] Tried using the PySimpleGUI.py file on GitHub. Your problem may have already been fixed but not released · Im try to figure out how to place a buttons on a window in different places beside how it default to where they are listed or next to each other. As in tkinter you can use place() which let you use x,y an places the object to where ever.
Author   Cyberfal1
Top answer
1 of 1
11

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()
🌐
Mouse Vs Python
blog.pythonlibrary.org › home › pysimplegui: using an image as a button
PySimpleGUI: Using an Image as a Button - Mouse Vs Python
September 7, 2021 - Learn how to use images as a custom button in your graphical user interface using PySimpleGUI and the Python programming language
🌐
PySimpleGUI
docs.pysimplegui.com › en › latest › call_reference › tkinter › elements › pre_defined_buttons
Pre-Defined Buttons - PySimpleGUI Documentation
ColorChooserButton( button_text, target = (555666777, -1), image_filename = None, image_data = None, image_size = (None, None), image_subsample = None, tooltip = None, border_width = None, size = (None, None), s = (None, None), auto_size_button = None, button_color = None, disabled = False, font = None, bind_return_key = False, focus = False, pad = None, p = None, key = None, k = None, default_color = None, visible = True, metadata = None, expand_x = False, expand_y = False )
🌐
iO Flood
ioflood.com › blog › pysimplegui
PySimpleGUI: Guide to Python GUI Development
February 7, 2024 - If the ‘Hello’ button is clicked, we print ‘Hello, World!’. The loop continues until the window is closed. This is just a basic introduction to PySimpleGUI. There’s so much more to learn about this powerful library, including how to add different types of widgets, handle events, and create complex layouts.
🌐
PySimpleGUI
docs.pysimplegui.com › en › latest › cookbook › original › menus
Menus - PySimpleGUI Documentation
When you click on a menu item, you get back a "button" with that menu item's text, just as you would had that text been on a button.
🌐
GitHub
github.com › PySimpleGUI › PySimpleGUI › issues › 3646
[ Question] How do I get the button (option) that the user chose in a popup · Issue #3646 · PySimpleGUI/PySimpleGUI
November 20, 2020 - Hi I want to ask a question How do I get the button (option) that the user chose in a popup I leave an example code · import PySimpleGUI as sg sg.theme('DarkAmber') # Keep things interesting for your users layout = [[sg.Text('Persistent window')], [sg.Button('Read'), sg.Exit()]] window = sg.Window('Window that stays open', layout) while True: # The Event Loop event, values = window.read() if event == sg.WIN_CLOSED or event == 'Exit': break if event == 'Read': sg.PopupOKCancel('PopupOKCancel') if event == 'ok': print('the user pressed the OK button') if event == 'Cancel': print('the user pressed the Cancel button') window.close()
Author   shields1968
🌐
Mouse Vs Python
blog.pythonlibrary.org › home › pysimplegui - an intro to laying out elements
PySimpleGUI - An Intro to Laying Out Elements - Mouse Vs Python
January 25, 2022 - When you run this code, you will see the buttons aligned horizontally, with one next to the other going from left-to-right. Now you are ready to see how you can rewrite the code to create a vertical layout! To create a vertically oriented layout with PySimpleGUI, you need to make the layout ...