[pygtk] Callbacks

Peyman paskari007 at yahoo.ca
Tue Mar 24 22:04:31 WST 2009


Hi

I keep coming back to this, and it is literally one of the biggest  
problems bugging me. I have a button, attached to two callbacks via  
glade. Here is the code

def on_button1_press_event(widget,data,wtree):
	print "pressed"

def on_button1_release_event(widget,data,wtree):
	print "released"

and it works as expected when clicked. But when I change it to the  
following


def on_button1_press_event(widget,data,wtree):
	print "pressed"
	
	widget.handler_block_by_function(on_button1_release_event)

def on_button1_release_event(widget,data,wtree):
	print "released"

I get the following error

TypeError: nothing connected to <function on_button1_release_event at  
0x835e3ac>

How could nothing being connected if I know it's connected?

Cheers


Peyman Askari



On 11 Mar 2009, at 13:31, Walter Leibbrandt wrote:

> Hi,
>
> Maybe I should just let some code do the talking. See attached script.
>
> The button's "clicked" handler adds an "x" to the entry. The toggle  
> button uses handler_(un)block_by_func() on addx_clicked so that it  
> is only enabled if the toggle button is active. Note that  
> handler_(un)block_by_func() was called on btn, seeing as that is the  
> widget to which the handler is connected.
>
> This is quite an exciting discovery for myself too. Seeing as its in  
> GObject and not in Glade, I can use this with my custom widgets too  
> and need not keep signal dictionaries anymore! :)
>
> P.S. In this context "handler" means the function/method connected  
> to a signal.
>
> Peyman wrote:
>> Hi Walter
>>
>> I am running into problems using it. To use  
>> handler_bloc_by_func(callable) you have to pass callable: a  
>> callable python object. I have tried using the widget itself, and  
>> it's callback functions, but neither is working. What exactly do i  
>> have to pass as a parameter. You made reference to passing the  
>> "handler" to it.
>>
>> Cheers
>>
>>
>>
>> Peyman Askari
>>
>> On 11 Mar 2009, at 12:53, Walter Leibbrandt wrote:
>>
>>> Hi,
>>>
>>> It seems that this has been asked before: http://osdir.com/ml/gnome.gtk+.python/2003-04/msg00089.html
>>>
>>> Short answer: no, it doesn't seem like you can. I have, however,  
>>> found the handler_block_by_func() and handler_unblock_by_func()  
>>> methods of gobject.GObject. Although I haven't tested this yet, it  
>>> seems from the docs (http://library.gnome.org/devel/pygobject/stable/class-gobject.html#method-gobject--handler-block-by-func 
>>> ) that you can simply pass your handler to it to (un)block it from  
>>> being called.
>>>
>>> Let me know how/if it works.
>>>
>>> HTH,
>>>
>>> Peyman wrote:
>>>> I realize that once you call connect() it returns the handle_id.  
>>>> But if glade is doing this for me, are the handle id's stored  
>>>> somewhere? Surely there has to be a  
>>>> widget.get_handle_id('callback_function') method. I can't seem to  
>>>> find anything on this over the net though.
>>>
>>> -- 
>>> Walter Leibbrandt                  http://translate.org.za/blogs/walter
>>> Software Developer                                  +27 12 460  
>>> 1095 (w)
>>> Translate.org.za
>>>
>>> Recent blogs:
>>> * Firefox-style button with a pop-up menu
>>> http://www.translate.org.za/blogs/walter/en/content/firefox-style-button-pop-menu
>>> * Virtaal's MVCisation
>>> * Things that changed the way I code
>>>
>>>
>>
>
> -- 
> Walter Leibbrandt                  http://translate.org.za/blogs/ 
> walter
> Software Developer                                  +27 12 460 1095  
> (w)
> Translate.org.za
>
> Recent blogs:
> * Firefox-style button with a pop-up menu
> http://www.translate.org.za/blogs/walter/en/content/firefox-style-button-pop-menu
> * Virtaal's MVCisation
> * Things that changed the way I code
>
>
> #!/usr/bin/env python
>
> import gtk
> import gtk.glade
>
> xml = gtk.glade.XML('gui.glade')
> win = xml.get_widget('window')
> btn = xml.get_widget('button')
> entry = xml.get_widget('entry')
>
> def addx_clicked(*args):
>    entry.props.text += 'x'
>
> def enable_toggled(togglebtn):
>    if togglebtn.get_active():
>        btn.handler_unblock_by_func(addx_clicked)
>    else:
>        btn.handler_block_by_func(addx_clicked)
>
> win.connect('destroy', lambda *args: gtk.main_quit())
> btn.connect('clicked', addx_clicked)
> xml.get_widget('togglebutton').connect('toggled', enable_toggled)
>
> win.show_all()
> gtk.main()
> <gui.glade>



More information about the pygtk mailing list