Skip to content

Commit dcd7a3c

Browse files
committed
Updated the "realtime script launcher" demo to use the Exec APIs. Changed timeout error handling in execute_get_results
1 parent 8f20ef3 commit dcd7a3c

2 files changed

Lines changed: 24 additions & 41 deletions

File tree

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,31 @@
1-
import subprocess
2-
import sys
31
import PySimpleGUI as sg
42

53
"""
64
Demo Program - Realtime output of a shell command in the window
75
Shows how you can run a long-running subprocess and have the output
86
be displayed in realtime in the window.
7+
8+
Copyright 2022 PySimpleGUI
99
"""
1010

11-
sg.theme('Dark Blue 3')
1211

1312
def main():
14-
layout = [
15-
[sg.Output(size=(110,30), background_color='black', text_color='white')],
16-
[sg.T('Promt> '), sg.Input(key='-IN-', do_not_clear=False)],
17-
[sg.Button('Run', bind_return_key=True), sg.Button('Exit')] ]
18-
19-
window = sg.Window('Realtime Shell Command Output', layout)
20-
21-
while True: # Event Loop
22-
event, values = window.read()
23-
# print(event, values)
24-
if event in (sg.WIN_CLOSED, 'Exit'):
25-
break
26-
elif event == 'Run':
27-
runCommand(cmd=values['-IN-'], window=window)
28-
window.close()
29-
30-
31-
def runCommand(cmd, timeout=None, window=None):
32-
nop = None
33-
""" run shell command
34-
@param cmd: command to execute
35-
@param timeout: timeout for command execution
36-
@param window: the PySimpleGUI window that the output is going to (needed to do refresh on)
37-
@return: (return code from command, command output)
38-
"""
39-
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
40-
output = ''
41-
for line in p.stdout:
42-
line = line.decode(errors='replace' if (sys.version_info) < (3, 5) else 'backslashreplace').rstrip()
43-
output += line
44-
print(line)
45-
window.refresh() if window else nop # yes, a 1-line if, so shoot me
46-
47-
retval = p.wait(timeout)
48-
return (retval, output)
13+
layout = [
14+
[sg.Multiline(size=(110, 30), echo_stdout_stderr=True, reroute_stdout=True, autoscroll=True, background_color='black', text_color='white', key='-MLINE-')],
15+
[sg.T('Promt> '), sg.Input(key='-IN-', focus=True, do_not_clear=False)],
16+
[sg.Button('Run', bind_return_key=True), sg.Button('Exit')]]
17+
18+
window = sg.Window('Realtime Shell Command Output', layout)
19+
while True: # Event Loop
20+
event, values = window.read()
21+
if event in (sg.WIN_CLOSED, 'Exit'):
22+
break
23+
elif event == 'Run':
24+
sp = sg.execute_command_subprocess(values['-IN-'], pipe_output=True, wait=False)
25+
results = sg.execute_get_results(sp, timeout=1)
26+
print(results[0])
27+
28+
window.close()
4929

5030

5131
main()

PySimpleGUI.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/usr/bin/python3
2-
version = __version__ = "4.57.0 Released 13-Feb-2022"
2+
version = __version__ = "4.57.0.1 Unreleased"
33

44
_change_log = """
55
Changelog since 4.57.0 released to PyPI on 13-Feb-2022
66

7-
4.56.0.1
8-
7+
4.57.0.1
8+
Added checking for timeout error to execute_get_results instead of showing an error popup as it's not truly an error in this case
99
"""
1010

1111
__version__ = version.split()[0] # For PEP 396 and PEP 345
@@ -21133,6 +21133,9 @@ def execute_get_results(subprocess_id, timeout=None):
2113321133
# will get an error if stdout and stderr are combined and attempt to read stderr
2113421134
# so ignore the error that would be generated
2113521135
pass
21136+
except subprocess.TimeoutExpired:
21137+
# a Timeout error is not actually an error that needs to be reported
21138+
pass
2113621139
except Exception as e:
2113721140
popup_error('Error in execute_get_results', e)
2113821141
return out_decoded, err_decoded

0 commit comments

Comments
 (0)