Python : How do GTK widget instances work?
Question: This is driving me litterally crazy :

  * Why is the method `the_method(self, button)` referencing two separate instances of `self.button` depending on what object calls it?
  * How do I reference an object instance explicitly?

Thank you for your help

* * *
    
    
    import os, stat, time
    import gtk
    
    class Lister(object):
    
        OPEN_IMAGE = gtk.image_new_from_stock(gtk.STOCK_DND_MULTIPLE, gtk.ICON_SIZE_BUTTON)
        CLOSED_IMAGE = gtk.image_new_from_stock(gtk.STOCK_DND, gtk.ICON_SIZE_BUTTON)
    
        def __init__(self, dname = None):
            filename = "foo"
            self.hbox = gtk.HBox()
            self.button = gtk.Button()
            self.button.set_image(self.OPEN_IMAGE)
            self.button.connect('clicked', self.open_file)
            self.hbox.pack_start(self.button, False)
    
        def open_file(self, button):
            Buttons().the_method("foo")
            # return
    
    
    class Buttons(object):
    
        OPEN_IMAGE = gtk.image_new_from_stock(gtk.STOCK_DND_MULTIPLE, gtk.ICON_SIZE_BUTTON)
        CLOSED_IMAGE = gtk.image_new_from_stock(gtk.STOCK_DND, gtk.ICON_SIZE_BUTTON)
    
    
        def __init__(self):
    
            self.button = gtk.Button() # THIS is the button to modify
            self.hbox = gtk.HBox()
            self.hbox.pack_start(self.button, False)
            self.button.set_image(self.OPEN_IMAGE)
    
            self.button.connect('clicked', self.the_method)
    
        def the_method(self, button):
            print vars(self)
            self.button.set_image(self.CLOSED_IMAGE)
    
    
    class GUI(object):
    
        def delete_event(self, widget, event, data=None):
            gtk.main_quit()
            return False
    
        def __init__(self):
            self.window = gtk.Window()
            self.window.set_size_request(300, 600)
            self.window.connect("delete_event", self.delete_event)
    
            vbox = gtk.VBox()
            vbox.pack_start(Buttons().hbox, False, False, 1)
            vbox.pack_start(Lister().hbox)
    
            self.window.add(vbox)
            self.window.show_all()
            return
    
    def main():
        gtk.main()
    
    if __name__ == "__main__":
        GUI()
        main()
    


Answer: > Why is the method the_method(self, button) referencing two separate
> instances of self.button depending on what object calls it?

It does not depend on the caller: You are explicitly creating a new instance
of Buttons every time (e.g. `Buttons().hbox` creates a new instance and gets
the hbox from it).

> How do I reference an object instance explicitly?

You already refer to an instance, it's just a new instance every time. These
two calls will call the method on the same instance, as expected:

    
    
    my_buttons = Buttons(); 
    my_buttons.the_method();
    my_buttons.the_method();
    

This is the same in every object oriented language, so any OO tutorial could
be helpful but <http://docs.python.org/tutorial/classes.html> may help
understand how classes work in python.

