[wxPython-users] using wxpython with non-GUI applications

Aaron Brady castironpi at comcast.net
Wed Jan 9 12:50:44 PST 2008


> -----Original Message-----
> From: Mike Burnett [mailto:burnettmn at ornl.gov]
> Sent: Wednesday, January 09, 2008 1:48 PM
> Subject: [wxPython-users] using wxpython with non-GUI applications
> 
> I have python code that runs in a non-GUI environment (i.e., command
> line) for doing complex computations. I would like to display the
> periodic results of the computations graphically.  Wxpython includes
> the graphical tools I need to display the results, but it is designed
> for event driven windows applications.  Can wxpython be used for my
> minimal graphics needs, or is there some other tool that would be
> more appropriate?
> 
> Mike Burnett

Threads.  1) Run wx.PySimpleApp().MainLoop() in a separate thread, or (2)
run your code in another thread (mainloop is cpu time-friendly).  Note, use
PostEvent to get messages from your thread into the Gui event framework.
Just hide your form when.

import wx
import wx.lib.newevent

class AsyncCall:
	from threading import Event
	"""	Runs a callable through parent's thread of MainLoop.
	Code may wait() on self.complete for self.result to contain
	the result of callable(*args,**kwargs).  It is set upon
completion."""
	def __init__( self,parent,callable,*args,**kwargs ):
		(AsyncEvent, EVT_ASYNCCALL) = wx.lib.newevent.NewEvent()
		parent.Bind( EVT_ASYNCCALL, self.OnAsyncCall )
		self.result, self.complete = None, self.Event()
		self.callable, self.args, self.kwargs = callable, args,
kwargs
		wx.PostEvent( parent, AsyncEvent())
	def OnAsyncCall( self,evt ):
		self.result=self.callable(*self.args,**self.kwargs)
		self.complete.set()
	def Wait( self ):
		self.complete.wait()

Which IMO should appear in the library; the DelayedResult in the demo is not
very good.




More information about the wxpython-users mailing list