1
votes

I'm learning python and gtk and so trying to using cairo to draw a rectangle in the screen with the mouse (I just managed to draw a rectangle without mouse).

However something strange is happening since I'm receiving more arguments than what I'm passing. How is that possible?

draw_rectangle - method definition:

def draw_rectangle (self, widget, start_x_cood, start_y_cood, ending_x_cood, ending_y_cood):
    print ("draw_retangle")
    cr = cairo.Context ()
    cr.set_source_rgba(1, 1, 1, 1)
    cr.rectangle(start_x_cood, start_y_cood, ending_x_cood, ending_y_cood)
    cr.fill()

Method that calls draw_rectangle:

def on_motion_notify_event (self, widget, event):
        print("on_motion_notify_event")
        if event.is_hint:
            x, y, state = event.window.get_pointer()
        else:
            x = event.x
            y = event.y
            state = event.state

        if self.firstClick :
            self.ending_x_cood = x
            self.ending_y_cood = y
            self.draw_rectangle(self, widget, self.start_x_cood, self.start_y_cood, self.ending_x_cood, self.ending_y_cood)

        return True

This is giving me the following error:

on_motion_notify_event
Traceback (most recent call last): File "gui2.py", line 56, in on_motion_notify_event
self.draw_rectangle(self, widget, self.start_x_cood, self.start_y_cood, self.ending_x_cood, self.ending_y_cood)
TypeError: draw_rectangle() takes exactly 6 arguments (7 given)
on_motion_notify_event
Traceback (most recent call last):
File "gui2.py", line 56, in on_motion_notify_event self.draw_rectangle(self, widget, self.start_x_cood, self.start_y_cood, self.ending_x_cood, self.ending_y_cood)
TypeError: draw_rectangle() takes exactly 6 arguments (7 given)

Where is that 7th argument coming from? My searches are leading me to *args and **kwargs but it's not making much sense.

I've uploaded a runnable version of the code here

1
Also, there's no need to pass self.x, self.y etc from a method to another method inside the same class. - dursk
very true, good point! - Fawix

1 Answers

4
votes

Python passes self to instance methods for you, so:

self.draw_rectangle(self, widget, self.start_x_cood, self.start_y_cood, self.ending_x_cood, self.ending_y_cood)

actually is passing self twice. You want:

self.draw_rectangle(widget, self.start_x_cood, self.start_y_cood, self.ending_x_cood, self.ending_y_cood)