4

Trying out PySimpleGui for the first time, wanting to create an exec program that allows the user to either move or copy directories/files to the destination of their choice, but dont really understand how to link the action to the buttons.

My current program looks like this:

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, "Copy", button_color=("white", 
"green"), size=(6, 1)),sg.Exit(button_color=("white", "red"), 
size=(6, 1))]]

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

From what I am able to clearly understand, I would think that involving the copy command into the button's properties would link it to the command defined earlier in the code. I have src and dest blank as the inputs for src and dest and laid out with a browse folder extension added on for easier file management.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • I think the main program is misunderstanding that PySimpleGUI does not have a similar API and architecture to the major GUI frameworks. It does not follow their conventions. Instead of a callback model, like you're attempting, events are handled in a simple event loop. – Mike from PSG Mar 22 '19 at 17:11

1 Answers1

5

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])
Mike from PSG
  • 5,312
  • 21
  • 39