Hello everyone,
I'm working on Ubuntu 12.04 and creating a GUI. Now, at some point of
it I need to run some shell scripts at the click of a button that begin printing many notifications/output in
the command window. This all works fine, however, it would be great if
all that output can be printed in the GUI itself. Is there a way of
doing this? Sort of like cloning the command line into a "Static
Text" field or similar in the GUI.
I'm sending two simpler versions of my problem as samples alongwith.
1 - gui (wxpython code for the gui)
2 - script (the shell script which runs at the click of the button)
I'm also copy-pasting the shell-script here.
#!/bin/bash
#set -x
set -e
#output
cd
cd "$2"
types=$(file "$1")
size=$(stat -c '%s' "$1")
echo "$types"
echo "$size"
python -V
So, basically what I'm asking is how do I get the output of the last three lines on my GUI?
Any help would be appreciated. Also, please keep in mind I'm a noob, so please answer in detail. Also attach sample scripts if possible. Thankyou! :)
Regards,
Ankita.
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Watch out for some caveats of redirection with subprocess.Popen and PIPEs... if you're running your GUI with pythonw.exe, you may need to redirect stdin also (but if you don't actually need to send stdin from your GUI process to the script, you can close the PIPE just after you call Popen), since pythonw.exe doesn't come with the default std file handles (stdout, stderr, stdin).If the script/process doesn't terminate quickly, the communicate method of subprocess will block. This is often solved by spawning thread that just listens to the end of the PIPE, then uses wx.CallAfter to update the GUI.
I don't recall ever needing to redirect stdin. Even when I turned my scripts into exes.
# If the kernel is running on pythonw and stdout/stderr are not been# re-directed, it will crash when more than 4KB of data is written to# stdout or stderr. This is a bug that has been with Python for a very# long time; see http://bugs.python.org/issue706263.# A cleaner solution to this problem would be to pass os.devnull to# Popen directly. Unfortunately, that does not work.
Hey!
Thankyou Sunday, Nathan & Mike! It took me a while but here's what I did finally and it worked. I'm attaching the script alongwith and also copy-pasting a little part here.
def button1Click(self,event):
self.button1.Show()
proc = subprocess.Popen(['script', self.name, self.path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in proc.stdout:
wx.CallAfter(self.on_text, line)
self.button1.Disable()
def on_text(self, text):
self.text.AppendText(text)
Now, although I've successfully implemented this thanks to you guys; I'm still not completely sure about what I've done here(I've mentioned earlier, I'm a beginner!). So, could you please maybe send me some links to read more about the whole calling a subprocess, PIPE(what is this?!),
You probably haven't discovered the wxPython/Phoenix documentation yet, it is well explained in there:CallAfter.
--
You received this message because you are subscribed to a topic in the Google Groups "wxPython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/wxpython-users/Nk-6c1Z_0m4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to wxpython-user...@googlegroups.com.