reading input and output of wx.py.shell to a textctrl

Hi,
would possibly just redirecting sys.stdout (and probably sys.stderr)
work in this case?
cf.

You could use the AppendText(...) method of a textctrl instead of
WriteText(...) too.

You should maybe keep a reference to the original stdout (and stderr)
in some variable, in order to restore it easily again, if it would be
needed in the code to enable normal function of the shell.

However, I guess, this would be about the first posibility you tried,
hence if this won't work, some details or sample code might be needed
to see the problems and possible solutions.

hth,
  vbr

···

2014-08-07 5:22 GMT+02:00 Hemadri Saxena <hemadri.saxena@gmail.com>:

I have created a wx.py.shell in my code and now user is entering command and
getting response back in the shell itself.

Now on a button click I want to redirect all the commands(input) and
response(output) to textctrl. Can anybody help me here ??? I tried a lot
but its not working...

I have seen this link many times …This doesn’t work for shelll…There are many predefined functions of shell like readline() redirect etc…all somehow doesn’t work for me …

when I print sys.stdout its print some garbage

···

On Thu, Aug 7, 2014 at 1:16 PM, Vlastimil Brom vlastimil.brom@gmail.com wrote:

2014-08-07 5:22 GMT+02:00 Hemadri Saxena hemadri.saxena@gmail.com:

I have created a wx.py.shell in my code and now user is entering command and

getting response back in the shell itself.

Now on a button click I want to redirect all the commands(input) and

response(output) to textctrl. Can anybody help me here ??? I tried a lot

but its not working…

Hi,

would possibly just redirecting sys.stdout (and probably sys.stderr)

work in this case?

cf.

http://www.blog.pythonlibrary.org/2009/01/01/wxpython-redirecting-stdout-stderr/

You could use the AppendText(…) method of a textctrl instead of

WriteText(…) too.

You should maybe keep a reference to the original stdout (and stderr)

in some variable, in order to restore it easily again, if it would be

needed in the code to enable normal function of the shell.

However, I guess, this would be about the first posibility you tried,

hence if this won’t work, some details or sample code might be needed

to see the problems and possible solutions.

hth,

vbr

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.

For more options, visit https://groups.google.com/d/optout.


Thanks and Regards
Hemadri Saxena
9086087510

all somehow doesn’t work for me …

Hi Hemadri. People on this list are really helpful, but it’s even easier to help if you are as specific and as clear as you can be in terms of what you are observing when something “somehow doesn’t work”. For example, if you get an error, copy and paste the error into your question.

It seems like this essay is the standard one people are referred to on this issue:
http://www.catb.org/esr/faqs/smart-questions.html

In it, for example, it says: “saying ‘it doesn’t work’, will get you ignored”. So, post your code, post your errors, be specific, and let’s hope you can get some help.

Also, please bottom post, and not top post. See here:

http://www.idallen.com/topposting.html

Last: what is your overall goal? Why do you need to make yet another shell?

Che

Hi,
with the details, you provided now, some problems can be identified;
most importantly:
text = sys.stdout
won't work like you would probably indend - it just binds the name
text to the stdout object,
it won't call it, as there are no parens ...(), and even if it were
called, it wouldn't return the text content.

Check the mentioned sample

the important part is to replace the custom writable object as sys.stdout

        # redirect text here
        redir=RedirectText(log)
        sys.stdout=redir

in this way, it gets called whenever some output is neede (like in
response to print(...) etc.)

Furthermore,
<idlelib.rpc.RPCProxy object at 0x0000000002A8DD30>
is rather strange in this context
it likely originates form Idle - the builtin python ide/shell using
another gui - tkinter.

There can be various unwanted interactions between different gui
toolkits and this should normally be avioded.

hth,
  vbr

···

2014-08-07 20:18 GMT+02:00 Hemadri Saxena <hemadri.saxena@gmail.com>:

My goal is I have a Shell wx.py.shell and a textctrl...I want to grab all
the command response from shell(shell input and output).

For accomplishing this I want to control stdin, stdout and stderror of the
shell and do some processing(parsing) and send to the textctrl.

I tried the following :
1. Shell has many predefined functions which I used in code as
self.shell.readline() which print garbage as below. so this doesn't work for
me.

<idlelib.rpc.RPCProxy object at 0x0000000002A8DD30>

2. From link you were referring this is following class RedirectText as
below

class RedirectText(object):
    def __init__(self,aWxTextCtrl):
        self.out=aWxTextCtrl

def write(self, string):
    wx.CallAfter(self.out.WriteText, string)

# redirect text here
        redir=RedirectText(log)
        sys.stdout=redir

so I tried to use

text = sys.stdout (to read whats there in stdout)..but that doesn't work
either again garbage is return

Hope this make sense now.

Can

--

--
Thanks and Regards
Hemadri Saxena
9086087510

Hi,

···

On 7 August 2014 20:18, Hemadri Saxena hemadri.saxena@gmail.com wrote:

My goal is I have a Shell wx.py.shell and a textctrl…I want to grab all the command response from shell(shell input and output).

For accomplishing this I want to control stdin, stdout and stderror of the shell and do some processing(parsing) and send to the textctrl.

I tried the following :

  1. Shell has many predefined functions which I used in code as self.shell.readline() which print garbage as below. so this doesn’t work for me.

<idlelib.rpc.RPCProxy object at 0x0000000002A8DD30>

This doesn’t make any sense. A wxPython shell cannot possibly return a IDLE thing, it’s just weird. I don’t know the PyShell code so well but I would be surprised if you get an IDLE process inside a wx one.

You will have to show us a small, runnable sample app of what you have tried so far as we do not currently have a crystal ball to guess it. I’m sure you will get a ton more help if you did this.

Please do not top post.

Andrea.

“Imagination Is The Only Weapon In The War Against Reality.”
http://www.infinity77.net

-------------------------------------------------------------

def ask_mailing_list_support(email):

if mention_platform_and_version() and include_sample_app():
    send_message(email)
else:

    install_malware()
    erase_hard_drives()

-------------------------------------------------------------

Hi,

···

On 7 August 2014 21:17, Hemadri Saxena wrote:

Let me put my code and elaborate it more…

In shell I have defied a textctrl and bind my shell to a event when user press “enter”

self.LogArea = wx.TextCtrl(self, style=wx.TE_MULTILINE|wx.TE_READONLY)
self.shell.Bind(wx.EVT_KEY_DOWN, self.onEnter)

onEnter has the following code

def onEnter(self, event):

“”“”“”

keycode = event.GetKeyCode()

if keycode == wx.WXK_RETURN or keycode == wx.WXK_NUMPAD_ENTER:

print “you pressed ENTER!”

#commented code which I tried which is giving me garbage

#print self.shell.readline()

#print self.shell.redirectStdin()

#print self.shell.redirectStdout()

#self.LogArea .AppendText(self.shell.redirectStdout())

redir =RedirectText(self.LogArea )

sys.stdout=redir

#self.shell.prompt()

event.Skip()

Now when I run this it launches the Shell and on prompt I entered “Print “hello world”” this print “hello world” on the shell it self and on logarea (textctrl) it printed “you pressed ENTER!”…

I want to control the shell input and output…I want my logarea (textctrl) to print “hello world” instead of “you pressed ENTER!”

Is there anyway to redirect the shell input/output

I guess my written English is not good enough here:

  1. Please do not top post:

top-post: n., v.

[common] To put the newly-added portion of an email or Usenet response before the quoted part, as opposed to the more logical sequence of quoted portion first with original following.

  1. The code you posted is not a runnable sample. You should not instantiate a RedirectText class every time the user presses the Enter key, but rather initialize it in your class init method, whatever your class is. What is self.shell? How do we take your piece of code and run it standalone? That’s the purpose of a small, runnable sample app, and it’s the only way we will be able to help you in fixing your problems.

Andrea.

“Imagination Is The Only Weapon In The War Against Reality.”
http://www.infinity77.net

-------------------------------------------------------------

def ask_mailing_list_support(email):

if mention_platform_and_version() and include_sample_app():
    send_message(email)
else:

    install_malware()
    erase_hard_drives()

-------------------------------------------------------------

FYI the web interface on mobile at least doesn't give any quoted text, so top/bottom posting is moot.

Vbr, you don't need to save the original stdout, as it is found in sys.__stdout__ (as is stderr).

That is irrelevant, not everyone reads wxPython-related emails via the
mobile. And then it depends on the mobile, my iPhone does the right things.
the rule always applies: do not top post.

···

On 8 August 2014 00:05, Nathan McCorkle <nmz787@gmail.com> wrote:

FYI the web interface on mobile at least doesn't give any quoted text, so
top/bottom posting is moot.

--
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.
For more options, visit https://groups.google.com/d/optout.

--
Andrea.

"Imagination Is The Only Weapon In The War Against Reality."

# ------------------------------------------------------------- #
def ask_mailing_list_support(email):

    if mention_platform_and_version() and include_sample_app():
        send_message(email)
    else:
        install_malware()
        erase_hard_drives()
# ------------------------------------------------------------- #

Vbr, you don't need to save the original stdout, as it is found in sys.__stdout__ (as is stderr).

Well, it may be ok, in most cases, but there are cases of multiple
manipulations with stdout ... the OP's scenario is likely one of
them, as pyshell changes it by itself and then it changes once more in
user code, cf. (in pyshell etc.):

import sys
sys.stdout

<wx.py.pseudo.PseudoFileOut object at 0x02B56CF0>

sys.__stdout__

<_io.TextIOWrapper name='<stdout>' mode='w' encoding='cp852'>

vbr

···

2014-08-08 0:07 GMT+02:00 Nathan McCorkle <nmz787@gmail.com>: