Python - 接收更多参数而不是传递?

时间:2015-10-02 03:11:22

标签: python python-2.7

我正在学习python和gtk,因此尝试使用cairo在屏幕上用鼠标绘制一个矩形(我只是设法绘制一个没有鼠标的矩形)。

然而,由于我收到的参数多于我传递的参数,因此发生了一些奇怪的事情。怎么可能?

draw_rectangle - 方法定义:

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()

调用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

这给了我以下错误:

  

on_motion_notify_event
  Traceback(最近一次调用最后一次):    文件" gui2.py",第56行,在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()只需要6个参数(给定7个)
  on_motion_notify_event
  追溯(最近的呼叫最后):
   文件" gui2.py",第56行,在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()只需要6个参数(给定7个)

第7个论点来自哪里? 我的搜索引导我* args和** kwargs,但它没有多大意义。

我已经上传了代码here

的可运行版本

1 个答案:

答案 0 :(得分:4)

Python将self传递给实例方法,所以:

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

实际上是两次传递self。你想要:

self.draw_rectangle(widget, self.start_x_cood, self.start_y_cood, self.ending_x_cood, self.ending_y_cood)
相关问题