executing python scripts within wx frames

Hi,
I'm trying to make one of those frames thats similar to the wx python
demo where a folder is filled with demo wx python scripts and there
is
one main script that opens the other ones in as different items in a
tree/notebook.
I've gotten most of it figured out except the part where the scripts
are
executed in a wx frame. The difference between what I'm trying to do
and
the wx python demo one is that mine are generic scripts that output
the
results to std out/error (or whatever gets displayed in IDLE). Does
anyone know how I can do this? Simulate a IDLE or python shell in one
of
the frames thats basically the output of whatever script has been
selected to run?
It would be better if the solution was platform independent too.
Thanks for any suggestions

I had no idea how to do this but just to learn I Googled "embed shell
wxpython" or something like that and it got me started. Below is code
that embeds a shell inside a wxPython window using the py.shell.Shell
class (didn't bother to use sizers just to keep it simpler to read).
Along the way to that, I found pyshell and PyCrust, so they are in
there, too, if you uncomment them, though the first is deprecated and
the second lives in its own frame.

The redirected stdout/err I got from Mike Driscoll's blog (thanks), here:

Maybe this can give you some pointers for what you need.

Che

#Boa:Frame:Frame1

import wx
import wx.lib.pyshell as pyshell
import sys
import wx.py as py

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1PANEL1,
] = [wx.NewId() for _init_ctrls in range(3)]

#Got from Mike's blog
class RedirectText(object):
    def __init__(self,aWxTextCtrl):
        self.out=aWxTextCtrl

    def write(self,string):
        self.out.write(string)

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(201, 112), size=wx.Size(567, 493),
              style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
        self.SetClientSize(wx.Size(559, 459))

        self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(559, 459),
              style=wx.TAB_TRAVERSAL)

        self.button1 = wx.Button(id=wxID_FRAME1BUTTON1,
              label=u'Print to stdoutt/err', name='button1', parent=self.panel1,
              pos=wx.Point(8, 8), size=wx.Size(104, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
              id=wxID_FRAME1BUTTON1)

    def __init__(self, parent):
        self._init_ctrls(parent)

        #comment these in to see a PyCrust (separate) frame
## crustFrame = wx.py.crust.CrustFrame(self.panel1,
pos=wx.Point(300,100))
## crustFrame.Show()

        #comment these in to see a pyshell (deprecated) embedded
## shell = pyshell.PyShellWindow(self.panel1,-1,pos=wx.Point(120,10),
## size=wx.Size(440,430))

        #this uses py.shell, which is used in PyCrust
        shell = py.shell.Shell(self.panel1, -1, pos=wx.Point(120,10),
          size=wx.Size(440,430))

        self.panel1.Layout()

        # redirect text here - Thanks to Mike Driscoll for this
        redir=RedirectText(shell)
        sys.stdout=redir

    def OnButton1Button(self, event):
        print 'hello, I am redirected from the stdout/err'
        print '2 + 2 = ', 2 + 2
        event.Skip()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = create(None)
    frame.Show()

    app.MainLoop()

···

On Tue, Jul 13, 2010 at 5:21 PM, astan <k12umm@gmail.com> wrote:

Hi,
I'm trying to make one of those frames thats similar to the wx python
demo where a folder is filled with demo wx python scripts and there
is
one main script that opens the other ones in as different items in a
tree/notebook.
I've gotten most of it figured out except the part where the scripts
are
executed in a wx frame. The difference between what I'm trying to do
and
the wx python demo one is that mine are generic scripts that output
the
results to std out/error (or whatever gets displayed in IDLE). Does
anyone know how I can do this? Simulate a IDLE or python shell in one
of
the frames thats basically the output of whatever script has been
selected to run?
It would be better if the solution was platform independent too.
Thanks for any suggestions

Thanks. I'll see if that works.

···

On Jul 14, 2:28 pm, C M <cmpyt...@gmail.com> wrote:

On Tue, Jul 13, 2010 at 5:21 PM, astan <k12...@gmail.com> wrote:
> Hi,
> I'm trying to make one of those frames thats similar to the wx python
> demo where a folder is filled with demo wx python scripts and there
> is
> one main script that opens the other ones in as different items in a
> tree/notebook.
> I've gotten most of it figured out except the part where the scripts
> are
> executed in a wx frame. The difference between what I'm trying to do
> and
> the wx python demo one is that mine are generic scripts that output
> the
> results to std out/error (or whatever gets displayed in IDLE). Does
> anyone know how I can do this? Simulate a IDLE or python shell in one
> of
> the frames thats basically the output of whatever script has been
> selected to run?
> It would be better if the solution was platform independent too.
> Thanks for any suggestions

I had no idea how to do this but just to learn I Googled "embed shell
wxpython" or something like that and it got me started. Below is code
that embeds a shell inside a wxPython window using the py.shell.Shell
class (didn't bother to use sizers just to keep it simpler to read).
Along the way to that, I found pyshell and PyCrust, so they are in
there, too, if you uncomment them, though the first is deprecated and
the second lives in its own frame.

The redirected stdout/err I got from Mike Driscoll's blog (thanks), here:wxPython - Redirecting stdout / stderr - Mouse Vs Python

Maybe this can give you some pointers for what you need.

Che

#Boa:Frame:Frame1

import wx
import wx.lib.pyshell as pyshell
import sys
import wx.py as py

def create(parent):
return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1PANEL1,
] = [wx.NewId() for _init_ctrls in range(3)]

#Got from Mike's blog
class RedirectText(object):
def __init__(self,aWxTextCtrl):
self.out=aWxTextCtrl

def write\(self,string\):
    self\.out\.write\(string\)

class Frame1(wx.Frame):
def _init_ctrls(self, prnt):
# generated method, don't edit
wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
pos=wx.Point(201, 112), size=wx.Size(567, 493),
style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
self.SetClientSize(wx.Size(559, 459))

    self\.panel1 = wx\.Panel\(id=wxID\_FRAME1PANEL1, name=&#39;panel1&#39;, parent=self,
          pos=wx\.Point\(0, 0\), size=wx\.Size\(559, 459\),
          style=wx\.TAB\_TRAVERSAL\)

    self\.button1 = wx\.Button\(id=wxID\_FRAME1BUTTON1,
          label=u&#39;Print to stdoutt/err&#39;, name=&#39;button1&#39;, parent=self\.panel1,
          pos=wx\.Point\(8, 8\), size=wx\.Size\(104, 23\), style=0\)
    self\.button1\.Bind\(wx\.EVT\_BUTTON, self\.OnButton1Button,
          id=wxID\_FRAME1BUTTON1\)

def \_\_init\_\_\(self, parent\):
    self\.\_init\_ctrls\(parent\)

    \#comment these in to see a PyCrust \(separate\) frame

## crustFrame = wx.py.crust.CrustFrame(self.panel1,
pos=wx.Point(300,100))
## crustFrame.Show()

    \#comment these in to see a pyshell \(deprecated\) embedded

## shell = pyshell.PyShellWindow(self.panel1,-1,pos=wx.Point(120,10),
## size=wx.Size(440,430))

    \#this uses py\.shell, which is used in PyCrust
    shell = py\.shell\.Shell\(self\.panel1, \-1, pos=wx\.Point\(120,10\),
      size=wx\.Size\(440,430\)\)

    self\.panel1\.Layout\(\)

    \# redirect text here \- Thanks to Mike Driscoll for this
    redir=RedirectText\(shell\)
    sys\.stdout=redir

def OnButton1Button\(self, event\):
    print &#39;hello, I am redirected from the stdout/err&#39;
    print &#39;2 \+ 2 = &#39;, 2 \+ 2
    event\.Skip\(\)

if __name__ == '__main__':
app = wx.PySimpleApp()
frame = create(None)
frame.Show()

app\.MainLoop\(\)

Do you need the full interactive interpreter in the window or do you just want to display the output of the scripts? If the latter then all you need to do is execute the script in a way that captures the stdout/stderr, and then write that text to something like a wx.TextCtrl.

···

On 7/13/10 2:21 PM, astan wrote:

Hi,
I'm trying to make one of those frames thats similar to the wx python
demo where a folder is filled with demo wx python scripts and there
is
one main script that opens the other ones in as different items in a
tree/notebook.
I've gotten most of it figured out except the part where the scripts
are
executed in a wx frame. The difference between what I'm trying to do
and
the wx python demo one is that mine are generic scripts that output
the
results to std out/error (or whatever gets displayed in IDLE). Does
anyone know how I can do this? Simulate a IDLE or python shell in one
of
the frames thats basically the output of whatever script has been
selected to run?
It would be better if the solution was platform independent too.

--
Robin Dunn
Software Craftsman

I'm trying to do both actually. A python interpreter that will also
display output of scripts.
Using py.shell seems superb but doing a .runfile() on my python
scripts and some of them are giving indentation errors and syntax
errors but when I run them normally (F5 using IDLE) they run without a
problem.
Any ideas how to fix these?
Thanks again

···

On Jul 15, 2:18 am, Robin Dunn <ro...@alldunn.com> wrote:

On 7/13/10 2:21 PM, astan wrote:

> Hi,
> I'm trying to make one of those frames thats similar to the wx python
> demo where a folder is filled with demo wx python scripts and there
> is
> one main script that opens the other ones in as different items in a
> tree/notebook.
> I've gotten most of it figured out except the part where the scripts
> are
> executed in a wx frame. The difference between what I'm trying to do
> and
> the wx python demo one is that mine are generic scripts that output
> the
> results to std out/error (or whatever gets displayed in IDLE). Does
> anyone know how I can do this? Simulate a IDLE or python shell in one
> of
> the frames thats basically the output of whatever script has been
> selected to run?
> It would be better if the solution was platform independent too.

Do you need the full interactive interpreter in the window or do you
just want to display the output of the scripts? If the latter then all
you need to do is execute the script in a way that captures the
stdout/stderr, and then write that text to something like a wx.TextCtrl.

--
Robin Dunn
Software Craftsmanhttp://wxPython.org

[[ This group prefers bottom-posting. See Top Posting and Bottom Posting and Why is Bottom-posting better than Top-posting ]]

I'm trying to do both actually. A python interpreter that will also
display output of scripts.
Using py.shell seems superb but doing a .runfile() on my python
scripts and some of them are giving indentation errors and syntax
errors but when I run them normally (F5 using IDLE) they run without a
problem.
Any ideas how to fix these?

  >>> print shell.runfile.__doc__
  Execute all commands in file as if they were typed into the
         shell.

I think that is the root of the problem. It's not going to execute the file as a standalone script, but rather as if all the lines were typed in the shell. You probably want to use Python's execfile built-in function instead. If you're doing it programatically then your code can do something like

  theShell.run("execfile('filename.py')")

otherwise the user of the shell can just type something like

  execfile('filename.py')

···

On 7/22/10 3:04 PM, astan wrote:

--
Robin Dunn
Software Craftsman