|
From: James P. R. <u32...@an...> - 2004-04-27 02:05:52
|
I have successfully run a simulation in VPython that had a pygtk
interface using threads. The main thread thread ran the GTK main loop,
then I had a thread for the simulation. I had issues quitting the
program (though I often also have the same issues when I'm not using
threading or pygtk), and there also seemed to be various quirks when I
tried running it on other platforms. This is the basic idea:
import threading
class simulator(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.runningEvent = threading.Event()
def run(self):
while 1:
self.runningEvent.wait()
# simulation stuff
def startStopCallback(widget, data):
if s.runningEvent.isSet():
s.runningEvent.clear()
else:
s.runningEvent.set()
gtk.threads_init()
# gtk setup stuff including a start/stop button
s = simulator()
s.start()
The gtk.threads_init() must be called if you are using pygtk and threading.
One thing that you need to ensure, if you are ever making GTK calls from
a thread other than the thread GTK is running in, you must use
gtk.threads_enter() and gtk.threads_leave() before and after you make
the call.
|