python - 如何使用 PySimpleGUI 将结果打印到命令行而不是弹出窗口?

标签 python pysimplegui

下面是来自 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...我可以输入一些内容来将这些值放在命令行上。

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

最佳答案

看看 PySimpleGUI GitHub 上的演示程序,您会发现其中有几个演示程序向您展示了如何“启动”脚本或 EXE。如果您想在窗口中实时查看命令的输出,则 this demo program会为你做的。还有另一种方法可以启动命令,但不会实时输出到窗口。

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

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()

关于python - 如何使用 PySimpleGUI 将结果打印到命令行而不是弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55910191/

相关文章:

python - 我可以在 PySimpleGUI slider 上设置低于 0.1 的分辨率吗?

python - 导入错误: No module named PySimpleGUI

python - 在 GAE 中使用 matplotlib

python - Django 过滤符合 ManyToMany 中特定条件的行数

python - 使用 GUI 分发计算机应用程序 (Python)

python-3.x - 在 PySimpleGUI 的打开窗口顶部显示弹出窗口

php - 从 php 调用 python 有 2 个返回值

当键为 "numeric string"时的 python ordereddict

python - 如何自定义pytest名称

python - 在 PySimpleGUI 窗口中插入图像作为背景