简体   繁体   English

使用 PySimpleGui,如何让按钮工作?

[英]Using PySimpleGui, how to get buttons to work?

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.第一次尝试 PySimpleGui,想要创建一个 exec 程序,允许用户将目录/文件移动或复制到他们选择的目的地,但并不真正了解如何将操作链接到按钮。

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.我将 src 和 dest 空白作为 src 和 dest 的输入,并添加了浏览文件夹扩展名以便于文件管理。

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.要执行您要查找的操作,请在从读取返回“复制按钮”事件时调用 copy。

I urge you to read through the docs to get an understanding how these calls to Button, etc, work.我敦促您通读文档以了解这些对 Button 等的调用是如何工作的。 http://www.PySimpleGUI.org 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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM