如何在PyGTK中向button.connect添加其他参数?

时间:2010-12-29 11:17:07

标签: python pygtk

我想将2个ComboBox实例传递给一个方法并在那里使用它们(例如,打印它们的活动选择)。我有类似以下内容:

class GUI():
  ...

  def gui(self):
    ...
    combobox1 = gtk.combo_box_new_text()
    # code for inserting some values into the combobox
    combobox2 = gtk.combo_box_new_text()
    # code for inserting some values into the combobox
    btn_new = gtk.Button("new")
    btn_new.connect("clicked", self.comboprint)

  def comboprint(self):
    # do something with the comboboxes - print what is selected, etc.

如何将combobox1和combobox2传递给“comboprint”方法,以便我可以在那里使用它们?让他们成为类字段(self.combobox1,self.combobox2)是唯一的方法吗?

2 个答案:

答案 0 :(得分:10)

做这样的事情:

btn_new.connect("clicked", self.comboprint, combobox1, combobox2)

并且在你的回调comboprint中应该是这样的:

def comboprint(self, widget, *data):
    # Widget = btn_new
    # data = [clicked_event, combobox1, combobox2]
    ...  

答案 1 :(得分:1)

我会通过制作combobox1和combobox2类变量来解决这个问题,如下所示:

class GUI():
  ...

  def gui(self):
    ...
    self.combobox1 = gtk.combo_box_new_text()
    # code for inserting some values into the combobox
    self.combobox2 = gtk.combo_box_new_text()
    # code for inserting some values into the combobox
    btn_new = gtk.Button("new")
    btn_new.connect("clicked", self.comboprint)

  def comboprint(self):
    # do something with the comboboxes - print what is selected, etc.
    self.combobox1.do_something

这样做的好处是,当另一个函数需要对这些组合框执行某些操作时,他们可以这样做,而无需将组合框作为参数传递给每个函数。