[pygtk] Question about connecting signals.

Miguel Ángel García magmaxlistas at gmail.com
Wed Nov 5 15:50:37 WST 2008


El Sun, 02 Nov 2008 03:58:43 -0600
Nathan Lundquist <morbid.concept en gmail.com> escribió:
What you need is not to have the code in "a file". You need to have the
code in "a class".

For example, real code:

class BTC_Project(gtk.VBox):
    def __init__(self, preferences):
        gtk.VBox.__init__(self)
        self.xml = gtk.glade.XML(Paths.glade_dir('project.glade'),
                   'anchor_project')
        self.xml.signal_autoconnect(self)
        [...]

    def do_row_activated(self, selection):
        [...]

def main():
    project = BTC_Project(None)
    window = gtk.Window()
    window.connect('destroy', lambda w: gtk.main_quit())

    window.add(project)
    window.show_all()

    gtk.main()

First of all (main), I build a BTC_Project. In this case, it is a VBox,
but it is not necessary. My BTC_Project loads a XML and searchs for a
widget in it ('anchor_project'). 
This widget is a container with a TreeView widget, which has the
signal "do_row_activated" and I associate it with
"self.xml.signal_autoconnect(self)". In this case I say: "Yes, is this
class which implements the method I said at glade design time".

There is another way to do it. You can see it in the main method:
   window.connect('destroy', lambda w: gtk.main_quit())
Well... Here I have an inline method (with lambda), but the
interesting thing is the "connect" method. There is no glade now, but
it does not mind. I am associating "destroy" event with my inline
method.

I hope to have solved your question.

Good luck! 


> Hello all.
> 
> I have a question about connecting signals.
> 
> Here is my situation. I'm building (or at least trying to) an app
> that will be used to create/edit SugarCRM modules. I'm building it a
> piece at a time so that I don't get ahead of myself.
> 
> For example, the SugarCRM Module Builder allows you to add new fields
> to a module (though not in a way I like which is why I'm making my
> app) so I've created a few fields with PyGTK. I can't think of how to
> explain this so I've attached a picture. What is attached was created
> with Glade.
> 
> Now, there is a signal connected to the 'Apply' button and when it is 
> clicked the method 'OnApply' is called and the appropriate metadata
> for the field is created. The code that loads the glade file and has
> the 'OnApply' method is all in a file called SugarFieldText.py.
> 
> For my app I import this file and add the field to the app. I can't 
> figure out how to connect a signal for the app to use with this
> field. I need the app to fetch the metadata generated when the Apply
> button is clicked. Basically my main app doesn't know when the Apply
> button is clicked. Only the widget in SugarFieldText.py knows when
> the Apply button is clicked.
> 
> Any help would be appreciated. If I haven't explained my situation 
> enough (which I feel may be the case) please let me know and I'll try 
> and explain further.


-- 
In a world without walls and fences, who needs Windows and Gates?


More information about the pygtk mailing list