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!
Redirect the stdout into a file and the read the file content into the
display window
Thanks,
Sunday Olutayo
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-users+unsubscribe@googlegroups.com
<mailto:wxpython-users+unsubscribe@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.
On Monday, July 14, 2014 7:56:36 AM UTC-5, Ankita Juneja wrote:
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 will need to call your shell script using Python’s subprocess module. You can use the subprocess module’s communicate() method to get information from the shell script and copy it to the text control. You can either just grab the information from the shell script and set the value of the text control or you can redirect stdout and print it to the text control.
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.
···
On Monday, July 14, 2014 6:15:10 AM UTC-7, Mike Driscoll wrote:
Hi Ankita,
On Monday, July 14, 2014 7:56:36 AM UTC-5, Ankita Juneja wrote:
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 will need to call your shell script using Python’s subprocess module. You can use the subprocess module’s communicate() method to get information from the shell script and copy it to the text control. You can either just grab the information from the shell script and set the value of the text control or you can redirect stdout and print it to the text control.
I don’t recall ever needing to redirect stdin. Even when I turned my scripts into exes.
Mike
···
On Monday, July 14, 2014 12:42:15 PM UTC-5, Nathan McCorkle wrote:
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.
# 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.
"
<details class='elided'>
<summary title='Show trimmed content'>···</summary>
On Monday, July 14, 2014 1:52:05 PM UTC-7, Mike Driscoll wrote:
</details>
pythonw.exe starts a daemon process which doesn’t have the normal access to the standard file descriptors. The only thing you would need to do in your script is to specifically set the 3rd fd for stdin:
"
···
On Monday, July 14, 2014 2:49:48 PM UTC-7, Nathan McCorkle wrote:
It
On Monday, July 14, 2014 1:52:05 PM UTC-7, Mike Driscoll wrote:
I don’t recall ever needing to redirect stdin. Even when I turned my scripts into exes.
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.
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?!), CallAfter. I would like to understand how this works, really(and then maybe go through that discussion you had in the posts)!
a ‘connection/link’ between two processes, but maybe this explains
it better:
You probably haven’t discovered the wxPython/Phoenix documentation
yet, it is well explained in there:
“Phoenix” is the next generation of wxPython and is still in
development, but a lot of the documentation for it can be taken as
is for wxPython ‘classic’, a lot/most of the differences between
classic and Phoenix are documented here:
Werner
···
Hi Ankita,
On 7/15/2014 8:07, Ankita Juneja wrote:
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.
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?!),
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?!),
a 'connection/link' between two processes, but maybe this explains
"Phoenix" is the next generation of wxPython and is still in
development, but a lot of the documentation for it can be taken as
is for wxPython ‘classic’, a lot/most of the differences between
classic and Phoenix are documented here:
[http://wxpython.org/Phoenix/docs/html/classic_vs_phoenix.html](http://wxpython.org/Phoenix/docs/html/classic_vs_phoenix.html)
[http://wxpython.org/Phoenix/docs/html/MigrationGuide.html](http://wxpython.org/Phoenix/docs/html/MigrationGuide.html)
Werner
–
You received this message because you are subscribed to a topic in the Google Groups “wxPython-users” group.