python classes and signal connects


I created a python (new style) class which contains a Gtk.Button and a
callback function which is connected to the "clicked" signal:

class TestSignals(object):
    def __del__(self):
        print "Deleted object"

    def __init__(self):
        self.button = Gtk.Button("My Button")
        self.button.connect("clicked", self.button_clicked_cb)

    def button_clicked_cb(self, button, user_data):
        print "button clicked"

When I create instances of this class in a loop, the objects are never
destroyed (I never see "Deleted object" on the terminal and the memory
consumption increase):

if __name__ == "__main__":
    for i in range(0, 500):
        t = TestSignals()

    print "instance generation finished (count: %s)" % (i)
    raw_input("press enter to exit ...")

I can move the button_clicked_cb() function outside of the class (and
remove the "self" parameter) and then the instances will be destroyed.

Is this the correct way to handle this? Or is there something wrong with
pyi? Imho the callback should be inside of the class.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]