第一次尝试使用PySimpleGui,想创建一个可执行程序,允许用户将目录/文件移动或复制到他们选择的目标位置,但不太明白如何将操作链接到按钮。
我的当前程序如下所示:
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()
从我能够清晰理解的内容来看,我认为将复制命令纳入按钮属性中会将其链接到先前在代码中定义的命令。我将src和dest输入留空,并添加浏览文件夹扩展以便更轻松地管理文件。