0

I hope you are doing great this new year, I am creating a GUI program (Python3 Tkinter) which displays the output of another python script into a big entry.

Situation: My GUI has only 1 button which execute this command: python external_python_script.py -h

However I want the whole output of this command execution to be displayed into an entry, and not into the CMD.

So far this is my solution, but not effective:

command_textbox = os.system(python external_python_script.py -h) #simply execute the code in brackets

script_output.insert('1.0',command_textbox) #the whole output of the script will be displayed in this entry

What happened when I did this?

In the GUI program, when I pressed the button, into the Entry it is displayed only this 0

Any help is greatly appreciated, thank you!

  • 1
    ***`0`***: From `os.system` you get only the return code, use [subprocess.run](https://docs.python.org/3/library/subprocess.html#subprocess.run) instead. – stovfl Jan 01 '20 at 17:08
  • 1
    `os.system` returns the exit status of the command it executed, not some output. Rather than calling your python script this way, couldn't you rather import it? – Thierry Lathuille Jan 01 '20 at 17:09
  • 2
    Take a look at the code in the `errorwindow3k.py` module in my answer to the question [Display the output of the program on GUI with tkinter?](https://stackoverflow.com/questions/48977473/display-the-output-of-the-program-on-gui-with-tkinter) It uses `subprocess.Popen` instead of `os.system` (as @stovfl suggests). – martineau Jan 01 '20 at 17:32
  • @ThierryLathuille: Executing a non-tkinter script that prints its output from one using it to create a GUI (however that is done) would likely _not_ display the output in the GUI. – martineau Jan 01 '20 at 17:42
  • Clearly not, but the parts of the script that produce the output could be used in a cleaner way – Thierry Lathuille Jan 01 '20 at 18:07
  • Thank you guys so much, I finally solved my problem. Yeah, subprocess is the solution. Here is my code which solves it: ```final_command = subprocess.run(command, capture_output=True)``` ```output_box.insert('1.0',final_command)``` – Kleiton Kurti Jan 02 '20 at 16:46

1 Answers1

0

Thank you guys for helping me, Subprocess is the solution. So I put only these 2 lines of code and I got the solution I wanted:

final_command = subprocess.run(command, capture_output=True)

output_box.insert('1.0',final_command)