2

can you guys help me to know how to connect a button in my PySimpleGui script which will execute another python script when the run button is pressed/clicked.

For now, i've been reading about Subprocess and command = os.popen in a GUI script.

layout = [[ sg.Text('Click the button to launch Program')],
           [sg.Button('Launch')]]

win1 = sg.Window('My new window').Layout(layout)


win2_activate = False

while True:
    ev1, vals1 = win1.Read()
    if ev1 is None or ev1 == 'Cancel':
        break

    if not win2_activate and ev1 == 'Launch':
        win1.Hide()
        win2_activate = True
        layout2 = [[sg.Text('Report Auto')],
                    [sg.Input(do_not_clear=True)],
                    [sg.Text('', key='_OUTPUT_')],
                    [sg.Button('Run'), sg.Button('Cancel')]]

        win2 = sg.Window('Window2').Layout(layout2)
        while True:        
            ev2, vals2 = win2.Read()
            if ev2 is None or ev2 =='Cancel':
                win2_activate = False
                win2.Close()
                win1.UnHide()
                break

In my pysimplegui script, i have not yet included the subprocess or any library because i just don't know where to do it. Any help will is most welcome!

  • There are several Demo Programs located on the project's GitHub that show you how to launch programs from a PySimpleGUI based program. The program you've posted opens 2 windows. Are you trying to emulate launching a program? The Demo_Desktop_Floating_Toolbar.py launches programs using subprocess.Popen. Demo_Script_Launcher_Realtime_Output.py shows the launched program's output in the window. There are another 5 or 6 other examples there. – Mike from PSG Jul 25 '19 at 13:08
  • Thanks a lot! On my way to check it out! – Gaël Latouche Jul 26 '19 at 06:21
  • Feel free to open an Issue on that GitHub should you have any problems. – Mike from PSG Jul 26 '19 at 23:38
  • Does the answer posted do what you're looking for? – Mike from PSG Jul 28 '19 at 20:57
  • 1
    There is also an [API for executing commands](https://pysimplegui.readthedocs.io/en/latest/call%20reference/#exec-apis) in PySimpleGUI now. – rtrrtr Jul 23 '21 at 16:28

1 Answers1

1

Here's a full answer to your question, a PySimpleGUI program. This program allows you to type in a command. Then press a button. When button pressed the command is "run" and the output is shown in the window.

import subprocess
import sys
import PySimpleGUI as sg

def main():
    layout = [  [sg.Text('Enter a command to execute (e.g. dir or ls)')],
                [sg.Input(key='_IN_')],             # input field where you'll type command
                [sg.Output(size=(60,15))],          # an output area where all print output will go
                [sg.Button('Run'), sg.Button('Exit')] ]     # a couple of buttons

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

    while True:             # Event Loop
        event, values = window.Read()
        if event in (None, 'Exit'):         # checks if user wants to exit
            break

        if event == 'Run':                  # the two lines of code needed to get button and run command
            runCommand(cmd=values['_IN_'], window=window)

    window.Close()

# This function does the actual "running" of the command.  Also watches for any output. If found output is printed
def runCommand(cmd, timeout=None, window=None):
    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)                         # also return the output just for fun

if __name__ == '__main__':
    main()

Mike from PSG
  • 5,312
  • 21
  • 39