[wxPython-users] Calling parent functions from an embedded pyShell??

Patrick K. O'Brien pobrien at orbtech.com
Wed Nov 14 08:14:51 PST 2007


On Nov 13, 2007, at 6:30 PM, Patrick Van Pelt wrote:

> I think this is probably an easy one, but I just can't quite get my  
> head around it right now.  :-P
>
> I have an Aui app that has a pyShell embedded in one of the  
> dockable subpanels.  I'd like to be able to have certain commands  
> and functions (living in a sibling class) be available so that the  
> commands can be eexecuted from the shell.  What would be the best  
> way to achieve this?  I tried importing the sibling class into the  
> panel that has the shell object, but it couldn't access it that way  
> (couldn't get out of its own shell bubble, I assume).  I was able  
> to import the sibling class at runtime through the shell, but it  
> still didn't have any sense of the parent windows (some functions  
> and variables traverse across the panels).

You need to expose your commands to the environment/namespace in  
which the shell is executing.  The easiest way to do so is when you  
create the pyshell object by passing a dictionary as the "locals"  
argument.  Here is how it is done in the wxPython Demo application:

     def on_OpenShellWindow(self, evt):
         if self.shell:
             # if it already exists then just make sure it's visible
             s = self.shell
             if s.IsIconized():
                 s.Iconize(False)
             s.Raise()
         else:
             # Make a PyShell window
             from wx import py
             namespace = { 'wx'    : wx,
                           'app'   : wx.GetApp(),
                           'frame' : self,
                           }
             self.shell = py.shell.ShellFrame(None, locals=namespace)
             self.shell.SetSize((640,480))
             self.shell.Show()

             # Hook the close event of the main frame window so that we
             # close the shell at the same time if it still exists
             def CloseShell(evt):
                 if self.shell:
                     self.shell.Close()
                 evt.Skip()
             self.Bind(wx.EVT_CLOSE, CloseShell)

Hope that helps,

Pat

-- 
Patrick K. O'Brien
Orbtech    http://www.orbtech.com
Schevo     http://www.schevo.org






More information about the wxpython-users mailing list