1

下面是来自 PySimpleGUI 网站的示例代码,我喜欢它输出的 GUI,但不是让它在点击提交时弹出一个窗口,我希望它获取这些值并将它们放到命令行上并运行命令

import PySimpleGUI as sg

sg.ChangeLookAndFeel('GreenTan')

form = sg.FlexForm('Everything bagel', default_element_size=(40, 1))

column1 = [[sg.Text('Column 1', background_color='#d3dfda',     justification='center', size=(10,1))],
           [sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 1')],
           [sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 2')],
           [sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 3')]]
layout = [
    [sg.Text('All graphic widgets in one form!', size=(30, 1), font=("Helvetica", 25))],
    [sg.Text('Here is some text.... and a place to enter text')],
    [sg.InputText('This is my text')],
    [sg.Checkbox('My first checkbox!'), sg.Checkbox('My second checkbox!',     default=True)],
    [sg.Radio('My first Radio!     ', "RADIO1", default=True), sg.Radio('My second Radio!', "RADIO1")],
    [sg.Multiline(default_text='This is the default Text should you decide not to type anything', size=(35, 3)),
 sg.Multiline(default_text='A second multi-line', size=(35, 3))],
    [sg.InputCombo(('Combobox 1', 'Combobox 2'), size=(20, 3)),
 sg.Slider(range=(1, 100), orientation='h', size=(34, 20), default_value=85)],
    [sg.Listbox(values=('Listbox 1', 'Listbox 2', 'Listbox 3'), size=(30, 3)),
 sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=25),
 sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=75),
 sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=10),
 sg.Column(column1, background_color='#d3dfda')],
    [sg.Text('_'  * 80)],
    [sg.Text('Choose A Folder', size=(35, 1))],
    [sg.Text('Your Folder', size=(15, 1), auto_size_text=False, justification='right'),
 sg.InputText('Default Folder'), sg.FolderBrowse()],
    [sg.Submit(), sg.Cancel()]
 ]

button, values = form.Layout(layout).Read()
sg.Popup(button, values)

所以我的问题是最后一行 sg.Popup... 有没有我可以输入的东西来代替将这些值放在命令行上。

显然在这个例子中它不是一个创建的命令,但我最终会改变一些事情,以便用户输入将变成我想在命令行上运行的命令,所以此时我只需要知道如何在命令行而不是弹出窗口中获取所需的值。

4

1 回答 1

0

查看演示程序中的 PySimpleGUI GitHub,您会发现其中一些向您展示了如何“启动”脚本或 EXE。如果您想在窗口中实时查看命令的输出,那么这个演示程序将为您完成。还有另一个启动命令但不实时输出到窗口。

这是将执行的命令实时输出到窗口的程序:

import subprocess
import sys
import PySimpleGUI as sg

"""
    Demo Program - Realtime output of a shell command in the window
        Shows how you can run a long-running subprocess and have the output
        be displayed in realtime in the window.
"""

def main():
    layout = [  [sg.Text('Enter the command you wish to run')],
                [sg.Input(key='_IN_')],
                [sg.Output(size=(60,15))],
                [sg.Button('Run'), sg.Button('Exit')] ]

    window = sg.Window('Realtime Shell Command Output', layout)

    while True:             # Event Loop
        event, values = window.Read()
        # print(event, values)
        if event in (None, 'Exit'):
            break
        elif event == 'Run':
            runCommand(cmd=values['_IN_'], window=window)
    window.Close()


def runCommand(cmd, timeout=None, window=None):
    """ run shell command
    @param cmd: command to execute
    @param timeout: timeout for command execution
    @param window: the PySimpleGUI window that the output is going to (needed to do refresh on)
    @return: (return code from command, command output)
    """
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output = ''
    for line in p.stdout:
        line = line.decode(errors='replace' if (sys.version_info) < (3, 5) else 'backslashreplace').rstrip()
        output += line
        print(line)
        window.Refresh() if window else None        # yes, a 1-line if, so shoot me

    retval = p.wait(timeout)
    return (retval, output)


main()
于 2019-04-29T20:35:24.303 回答