embedding python in wxpython

5

5lvqbwl02

Hi, I've looked around for a way to allow a python console from within
a wxPython application, but have only found stuff on embedded/
extending python with C/C++ or wxWidgets in C++, but not wxPython.

Is this easy to do? Can someone point me in the right direction?

Also, typically when you embed a scripting language into a larger
application, how do you get the console environment to share data with
the larger application?

For instance, if the application has some gui stuff (for example
clicking on a object and dragging it around), how do you get
"object.select(x,y)" to print out on the console, and vice-versa: the
object gets selected if the user types "object.select(x,y)"?

I'd like the console to be a bidirectional representation of what's
going on in the gui, plus a general purpose evaluation environment
where you can manipulate application data via some api which is
automatically exposed to the console when the application opens up.

I'm looking for high-level hints/strategies/directions.

Thank you
Michael
 
M

Mike Driscoll

Hi, I've looked around for a way to allow a python console from within
a wxPython application, but have only found stuff on embedded/
extending python with C/C++ or wxWidgets in C++, but not wxPython.

Is this easy to do?  Can someone point me in the right direction?

Also, typically when you embed a scripting language into a larger
application, how do you get the console environment to share data with
the larger application?

For instance, if the application has some gui stuff (for example
clicking on a object and dragging it around), how do you get
"object.select(x,y)" to print out on the console, and vice-versa: the
object gets selected if the user types "object.select(x,y)"?

I'd like the console to be a bidirectional representation of what's
going on in the gui, plus a general purpose evaluation environment
where you can manipulate application data via some api which is
automatically exposed to the console when the application opens up.

I'm looking for high-level hints/strategies/directions.

Thank you
Michael

You should ask the guys on the wxPython list for pointers:
http://wxpython.org/maillist.php

I'm pretty sure I've seen it discussed there before. They already have
PyShell and PyCrumb that you could probably use within your program.
The wxPython includes demos of them and lots of other widgets.

Mike
 
S

Stef Mientki

Hi, I've looked around for a way to allow a python console from within
a wxPython application, but have only found stuff on embedded/
extending python with C/C++ or wxWidgets in C++, but not wxPython.

Is this easy to do? Can someone point me in the right direction?

Also, typically when you embed a scripting language into a larger
application, how do you get the console environment to share data with
the larger application?

For instance, if the application has some gui stuff (for example
clicking on a object and dragging it around), how do you get
"object.select(x,y)" to print out on the console, and vice-versa: the
object gets selected if the user types "object.select(x,y)"?

I'd like the console to be a bidirectional representation of what's
going on in the gui, plus a general purpose evaluation environment
where you can manipulate application data via some api which is
automatically exposed to the console when the application opens up.

I'm looking for high-level hints/strategies/directions.
If you're looking for "high-level",
I don't understand why you want to see what's going on in the gui,
as I see the gui just a tool to control the real data.
For a pretty high level data manipulation,
something like PyLab_Works ?
http://code.google.com/p/pylab-works/

cheers,
Stef
 
S

Steve Holden

Hi, I've looked around for a way to allow a python console from within
a wxPython application, but have only found stuff on embedded/
extending python with C/C++ or wxWidgets in C++, but not wxPython.

Is this easy to do? Can someone point me in the right direction?

Also, typically when you embed a scripting language into a larger
application, how do you get the console environment to share data with
the larger application?

For instance, if the application has some gui stuff (for example
clicking on a object and dragging it around), how do you get
"object.select(x,y)" to print out on the console, and vice-versa: the
object gets selected if the user types "object.select(x,y)"?

I'd like the console to be a bidirectional representation of what's
going on in the gui, plus a general purpose evaluation environment
where you can manipulate application data via some api which is
automatically exposed to the console when the application opens up.

I'm looking for high-level hints/strategies/directions.
I seem to remember you can create your wxApp with an argument of True or
False. One of those settings creates a window containing any output to
sys.stderr, if I remember rightly.

regards
Steve
 
J

Joe Strout

Steve said:
I seem to remember you can create your wxApp with an argument of True or
False. One of those settings creates a window containing any output to
sys.stderr, if I remember rightly.

You do -- True (the default) redirects standard output to a window,
while False does not redirect it.

However, neither setting will create a bidirectional console or
evaluation environment as the OP was asking for. (But other wx widgets
do provide that, as other replies have pointed out.)

Best,
- Joe
 
J

Joe Strout

Steve said:
I seem to remember you can create your wxApp with an argument of True or
False. One of those settings creates a window containing any output to
sys.stderr, if I remember rightly.

You do -- True (the default) redirects standard output to a window,
while False does not redirect it.

However, neither setting will create a bidirectional console or
evaluation environment as the OP was asking for. (But other wx widgets
do provide that, as other replies have pointed out.)

Best,
- Joe
 
M

Mike Driscoll

I seem to remember you can create your wxApp with an argument of True or
False. One of those settings creates a window containing any output to
sys.stderr, if I remember rightly.

regards
 Steve

That's true...or you can just redirect stdout, which is what the demo
does. Here's one way to do it:

<code>
import sys
import wx

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

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

class MyForm(wx.Frame):

def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "wxPython Redirect
Tutorial")

# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
log = wx.TextCtrl(panel, wx.ID_ANY, size=(300,100),
style = wx.TE_MULTILINE|wx.TE_READONLY|
wx.HSCROLL)
btn = wx.Button(panel, wx.ID_ANY, 'Push me!')
self.Bind(wx.EVT_BUTTON, self.onButton, btn)

# Add widgets to a sizer
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(log, 1, wx.ALL|wx.EXPAND, 5)
sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
panel.SetSizer(sizer)

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

def onButton(self, event):
print "You pressed the button!"


# Run the program
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyForm().Show()
app.MainLoop()
</code>

- Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top