【问题标题】:How do i print results to the command line instead of a popup window using PySimpleGUI?如何使用 PySimpleGUI 将结果打印到命令行而不是弹出窗口?
【发布时间】:2019-04-29 20:12:23
【问题描述】:

下面是来自 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... 有没有我可以输入的东西来代替将这些值放在命令行上。

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

【问题讨论】:

    标签: python pysimplegui


    【解决方案1】:

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

    【讨论】:

    • 我正在寻找更多关于使用 PySimpleGui 创建用户输入框,用户可以在其中输入有关任务的一些关键信息,然后输出使用这些输入变量并将其放入获取的命令中放置然后在命令行上运行。我不希望用户输入完整的命令。
    • 发布的示例向您展示了启动命令所需的一切。由您的应用程序代码来构建命令行,然后调用函数来执行它。我不是在尝试编写您的应用程序,而是向您展示它是如何完成的。您可以复制和粘贴大部分代码,然后对其进行修改以使用您要创建的“用户输入框”来构建命令行。
    • 这是某人发布的一些代码,可以满足您的需求......它从 GUI 收集了一些变量,然后构建一个命令字符串,然后执行该命令字符串:@987654322 @
    • 好吧,太好了,这让我到达了我想去的地方。两个问题,1 有没有一个网站只讨论 PySImpleGUI 可以做的所有例程以及如何使用它们? 2、PySImpleGUI中有没有一个例程,可以输入文本或者从之前的条目中选择?
    • 这里是文档:PySimpleGUI.org。 GitHub 是PySimpleGUI.com,您可以在其中发布问题以获取问题的解答。有大量文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多