Recreating wxPython app.MainLoop()

75 views
Skip to first unread message

Marc Dufour

unread,
Apr 13, 2010, 11:01:05 AM4/13/10
to wxPython-users
I've been working on a server-like wxPython program which uses PyRO
(Python Remote Objects), and I needed to repeatedly call the PyRO
service loop as the program runs.

I did not want to use a timer; ideally, it should have been called
within the main wx application loop (MainLoop()).

Trouble is, MainLoop() only returns when you quit the application, so
I had to replicate the MainLoop functionnality and insert the PyRO
service loop call within.

After some trial-and-error, I found that the following will do:

Run=1
while Run:
app.Yield()
app.ProcessIdle()
daemon.handleRequests(0)

("Yield". What an obvious method name for what it does - no wonder I
had to use trial and error...).

Of course, if anyone sees any pitfalls and has the proverbial "better
way to do it", I'm all ears.

Robin Dunn

unread,
Apr 13, 2010, 1:18:48 PM4/13/10
to wxpytho...@googlegroups.com

Using Yield in this way will result in an error if any of the event
handlers also need to call Yield (because nested yields are not allowed.)

Instead you can override MainLoop with your own implementation as shown
in samples/mainloop/mainloop.py or you can decouple things a bit more
and run your PyRO daemon in another thread, using wx.CallAfter or some
other means to safely pass info back to the GUI thread.


--
Robin Dunn
Software Craftsman
http://wxPython.org

Reply all
Reply to author
Forward
0 new messages