[pygtk] I want to make a example app with two thread and events

John Stowers john.stowers.lists at gmail.com
Tue Nov 16 04:34:25 WST 2010


On Mon, 2010-11-15 at 12:21 +0100, MD or MD wrote:
> Hi.
> 
> I am a spanish programmer and I start with pyGtk.
> 
> And I try to make a example that it is a simple window that blink a
> button with two colours, for test events and threads. But I don't know
> how to send custom event.

Calling GTK code from multiple threads will cause crashes. Don't do it
without protection. There are many solutions to this problem, Google
"pygtk threading" or see
http://faq.pygtk.org/index.py?req=show&file=faq20.006.htp


> 
> 
> 	def mThread(self):
> 		while True:
> 			print "Thread"
> 			#self.emit('custom_event')
> 			time.sleep(5)
> 

This function runs in the thread you created. self.emit calls all signal
handlers immediately, which results in code being executed from *this*
thread, and not the main (gtk.main) one. This will cause crashes unless
the advice given at the start is taken (i.e. idle_add emission, take the
gdk lock, etc)

Although the main problem is that you are trying to send a GObject
signal, self.emit("signal"), but your object does not inherit from
GObject. See __gsignals__ in here

http://www.pygtk.org/articles/subclassing-gobject/sub-classing-gobject-in-python.htm

And a threaded signal example here

http://www.johnstowers.co.nz/blog/index.php/2007/03/12/threading-and-pygtk/

John

> 
> 
> if __name__ == "__main__":
> 	test = test()
> 	test.main()
> --------------------------------------------------------------------
> 
> Bye and thanks.
> _______________________________________________
> pygtk mailing list   pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://faq.pygtk.org/




More information about the pygtk mailing list