Ok, thanks ( ^ _ ^ ) . At the end I create a example that run fine:
import pygtk
pygtk.require('2.0')
import gtk
import gobject
import threading
import time
gobject.threads_init()
class Test(threading.Thread):
def __init__(self):
super(Test, self).__init__()
self.quit = False
self.counter = 0
self.toggle = False
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("delete_event", self.delete_event)
self.window.connect("destroy", self.destroy)
self.window.set_border_width(10)
self.button = gtk.Button("Hello World")
self.button.connect("clicked", self.hello, None)
self.window.add(self.button)
self.button.show()
self.window.show()
def run(self):
while not self.quit:
gobject.idle_add(self.update_button)
self.button.emit("clicked")
time.sleep(1)
def update_button(self):
if self.toggle:
self.button.modify_bg(gtk.STATE_NORMAL,
gtk.gdk.Color(65535,0,0))
else:
self.button.modify_bg(gtk.STATE_NORMAL,
gtk.gdk.Color(0,65535,0))
self.toggle = not self.toggle
def destroy(self, widget, data=None):
gtk.main_quit()
def delete_event(self, widget, event, data=None):
print "delete event occurred"
return False
def main(self):
self.start()
gtk.main()
def hello(self, widget, data=None):
self.counter = self.counter + 1
print("Hello " + str(self.counter))
if __name__ == "__main__":
hello = Test()
hello.main()
2010/11/15 John Stowers <[email protected]>:
> 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 [email protected]
>> http://www.daa.com.au/mailman/listinfo/pygtk
>> Read the PyGTK FAQ: http://faq.pygtk.org/
>
>
>
_______________________________________________
pygtk mailing list [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/