[IPython-user] simple pointers for ipython -[wqt]thread?

Zachary Pincus zachary.pincus@yale....
Wed Jun 11 11:11:55 CDT 2008


Hello all,

My goal is to build a GUI window (really, just an opengl canvas) that  
I can interact with from an interactive Python interpreter. (Like what  
matplotlib offers, but really stripped down.)

I understand that IPython offers some support for things like this via  
the -wthread (and other -*thread  options), but after digging through  
a lot of the code and looking online for examples, I'm still a bit  
puzzled in terms of where to go.

Basically, I can get some demo code running by having a script called  
'wxtest.py' that builds a basic window (see below), and then running:
zach> ipython -wthread
In [1]: run wxtest

So now the window appears, and can be interacted with. Moreover, I can  
interact with the window via the interpreter:
In [2]: app.frame.SetTitle('NewTitle')

and so forth.

However, from what I can tell, this sort of interaction is dangerous:  
many operations (especially openGL) crash if they are called from a  
thread that isn't the one with the GUI context, no? It seems like I  
need some way of putting function invocations or whatnot in the  
runcode queue that gets managed by the background thread, right? (This  
appears to be what the 'run' magic command does.)

Is there any simple way to get access to that? Or is that all somehow  
taken care of already?

Thanks,
Zach



contents of wxtest.py, for reference:

import wx

class MyFrame(wx.Frame):
     def __init__(self, parent, ID, title, **kws):
         wx.Frame.__init__(self, parent, ID, title, **kws)
         panel = wx.Panel(self, -1)
         button = wx.Button(panel, 1003, "Close Me")
         button.SetPosition((15, 15))
         self.Bind(wx.EVT_BUTTON, self.OnCloseMe, button)
         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
     def OnCloseMe(self, event):
         self.Close(True)
     def OnCloseWindow(self, event):
         self.Destroy()

class MyApp(wx.App):
     def OnInit(self):
         self.frame = MyFrame(None, -1, "Hello from wxPython")
         self.frame.Show(True)
         self.SetTopWindow(self.frame)
         return True

app = MyApp(0)
app.MainLoop()



More information about the IPython-user mailing list