0

I have a python script I made a couple of months ago that is command line based. I want to make an optional GUI for it. It is a fairly basic program, it fetches cue files for game roms. I coded the GUI separately, and it came to mind that instead of trying to implement it into the code of the program, it'd be 10 times easier to just execute the program with the flags the user specified on the GUI, then print the output on a text field. This is the GUI:

program gui

The program parses flags with the argparse library. It has a positional argument which is the directory, and two optional flags being -r and -g (I guess you can identify which is which). How could I do it?

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
tralph3
  • 452
  • 4
  • 13
  • 1
    You could use `subprocess` module to get the content on the terminal.And use `Text.insert` to insert the text. – jizhihaoSAMA Jul 06 '20 at 01:41
  • I don't know how to use it. I can see how I can use it to call the script, but how do I get its output? – tralph3 Jul 06 '20 at 01:57
  • You could refer to this [qustion](https://stackoverflow.com/questions/89228/calling-an-external-command-from-python/89243#89243). – jizhihaoSAMA Jul 06 '20 at 02:01
  • I can't seem to be able to run the script. I tried like this: "subprocess.run(["python3 CueMaker.py"])" and also "subprocess.run(["CueMaker.py"])" both tell me there's no file or directory with that name. I checked multiple times with the os module that I was on the right directory with os.getcwd() and I was, so I don't know why it can't read it. Also, this doesn't tell me how to access its output. – tralph3 Jul 06 '20 at 03:16
  • Okay I know why it was giving me that error, I need to separate the commands on the run method like so: subprocess.run(["python3", "CueMaker.py"]). Now, I need to get the console output to insert it into tkinter's text widget. – tralph3 Jul 06 '20 at 03:23

1 Answers1

0

You can use subprocess.check_output() to get the output of a subprocess. Here's sample usage:

var = subprocess.check_output(
    ['python3', 'CueMaker.py'], shell=True, stderr=subprocess.STDOUT
)
# redirects the standrad error buffer to standrad output to store that in the variable.`

Then I can use var to change the text, assuming there's a StringVar() tied to the widget:

stringVar.set(var)
tralph3
  • 452
  • 4
  • 13