0

well basically what i am trying to do is that i want to show the output being executed in a graphical interface. i have a bash script that i am running using following code

from subprocess import call
myscript= call("Myscript.sh", shell=True)

Now this Myscript performs some set of tasks. i want to show everything that is happening in output to a graphical interface. some logic that i was trying to do here is by storing all the output in a string and printing it but it displays the final conlusion and its an integer value! i want all the strings to be printed of myscript working. code is as following that i tried :

root = Tk()
words=[]

a= call("sh amapt.sh", shell=True)
w = Label(root, text=words.append(a))
w.pack()
print w
root.mainloop()

This is my script output that i want to be displayed in a Gui somewhere as in label or form. i dont know how. im confused please guide me

Alex
  • 31
  • 8

1 Answers1

0

call returns the exit code, which is an integer value. Try check_output, which returns the text.

from subprocess import check_output

root = Tk()

# to get the error message you need to catch stderr too
a = check_output("sh amapt.sh; exit 0", shell=True, stderr=subprocess.STDOUT)
w = Label(root, text=a)
w.pack()
root.mainloop()
Novel
  • 13,406
  • 2
  • 25
  • 41
  • subprocess.CalledProcessError: Command 'Myscript' returned non-zero exit status 255 Now what? – Alex Jul 16 '17 at 02:19
  • That means 'Myscript' failed to run correctly. Fix whatever is preventing Myscript from running. – Novel Jul 16 '17 at 02:20
  • script is running perfectly and doing its work. – Alex Jul 16 '17 at 02:23
  • its fails to run with this check_output – Alex Jul 16 '17 at 02:24
  • `call` and `check_output` both pass the work to `Popen`, so if it works in one it should work in the other... I can't really help you with that further without seeing what you are trying to run. [Read the docs](https://docs.python.org/3/library/subprocess.html#subprocess.check_output) and make sure you are passing the variables correctly. – Novel Jul 16 '17 at 02:33
  • i have edited my question please do check the image i pasted in question. its the ouput of my script. that is running perfectly with call(myscript). But it fails to run with check_output, it give this error with check_ output subprocess.CalledProcessError: Command 'Myscript' returned non-zero exit status 255 – Alex Jul 16 '17 at 02:38
  • Oh, so you are specifically trying to see an error? Ok, I updated my answer (with code straight from the docs), but I have the feeling that whatever you are doing there is a better way to do it. – Novel Jul 16 '17 at 02:49
  • i am trying to get the whole program output to a gui dialog – Alex Jul 16 '17 at 02:52