[twisted+wxPython] widgets do not work?

Cliff Wells clifford.wells at comcast.net
Wed Oct 13 12:35:43 CEST 2004


On Tue, 2004-10-12 at 19:58 +0200, Taki Jeden wrote:
> Hi
> 
> Anybody used wxPython with twisted?
> 
> I started putting together a Twisted-based app with wx GUI, and the widgets
> just don't work - some controls do not show up etc. - at least on my
> system.
> 
> For example, if I just take the wxdemo.py shipped with Twisted, and add to
> it a static text, it doesn't show - while if I change 
>         reactor.registerWxApp(app)
>         reactor.run(0)
> into 
>         app.MainLoop(0)
> the text is there. What the heck is wrong with this? Or am I doing something
> wrong? Does the same work at yours?

The method I use to mix the two is to poll Twisted during wxPython's
idle event:

class SomeTwistedStuff:	                                                                                                                                                              
    def fetch(self, ...):
        reactor.startRunning()
        # do stuff here, i.e. set up Deferreds, etc
         
    def poll(self):
        if reactor.running:
            reactor.runUntilCurrent()
            reactor.doIteration(0)
        return reactor.running
     
   def pending(self):
        return reactor.running

class Frame(wx.Frame):
    def __init__(self, ...):
        ...
        self.twistedthing = SomeTwistedStuff(...)
	self.Bind(wx.EVT_IDLE, self.OnIdle)
        self.twistedthing.fetch(...)

    def OnIdle(self, event):
        if self.twistedthing.poll():
            event.RequestMore()
        event.Skip()

        
I yanked this out of a working app, so hopefully I didn't omit anything
important.  You'll of course probably want to pass callbacks/errbacks
from your wxPython portion of the app to the Twisted portion so that the
GUI can be synchronized with the Twisted backend.

I've also seen examples of doing the polling using a wx.Timer, but this
does a lot of unnecessary polling if Twisted doesn't have anything
available.  Probably not a lot of overhead, but certainly unnecessary
overhead in most cases.

Note that I tried using the wxPython reactor but found that so much time
was spent in Twisted that the GUI became unacceptably slow.

Regards,
Cliff

-- 
Cliff Wells <clifford.wells at comcast.net>




More information about the Python-list mailing list