Search code examples
pythonpygtksubclassing

pyGTK subclassing issue. Double self passing to the method


I'm trying to subclass gtk.DrawingArea. Here's the problem part of the code.

class ClusterGraph(gtk.DrawingArea):
    def __init__(self):
        super(ClusterGraph, self).__init__()
        self.add_events(gtk.gdk.BUTTON_PRESS_MASK)
        self.connect('button-press-event', self.on_mouse_dn)

    def on_mouse_dn(*args):
        print args

window = gtk.Window()
window.connect("destroy", gtk.main_quit)
window.set_default_size(300, 600)
cg = ClusterGraph()
window.add(cg)
window.show_all()
gtk.main()

The problem is that the instance is passed to the method twice.

on click it prints:

(<ClusterGraph object at 0x30167d8 (GtkDrawingArea at 0x2531610)>, <ClusterGraph object at 0x30167d8 (GtkDrawingArea at 0x2531610)>, <gtk.gdk.Event at 02F75F08: GDK_BUTTON_PRESS x=164,00, y=354,00, button=1>)

and my callback actually is equivalent for

def on_mouse_dn(self, self, event)

How to solve this problem? Or is it normal!?

by the way, why it prints

<ClusterGraph object at 0x30167d8 (GtkDrawingArea at 0x2531610)>

and not something like

<ClusterGraph object at 0x30167d8 (ClusterGraph at 0x2531610)>

Edit: The question is how to remove the extra argument.


Solution

  • This problem is solved in such way..

    GTK invokes callback in this way: cb(widget,event) where widget is instance of some widget class. So I needed explicit instance referense. This can be achieved by executing method from class obj.

    self.connect('button-press-event', self.on_mouse_dn)
    

    shall be replaced with

    self.connect('button-press-event', self.__class__.on_mouse_dn)
    

    or

    self.connect('button-press-event', ClusterGraph.on_mouse_dn)
    

    The first form is more flexible.