开发者

How do I add additional arguments to button.connect in PyGTK?

I want to pass 2 ComboBox instances to a a method and use them there (e.g., print their active selection). I have something similar to the following:

class GUI():
  ...

  def gui(self):
    ...
    combobox1 = gtk.combo_box_new_text()
    # code for inserting some va开发者_如何学Pythonlues 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.

How can I pass combobox1 and combobox2 to the method "comboprint", so that I can use them there? Is making them class fields (self.combobox1, self.combobox2) the only way to do this?


do something like this:

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

and in your callback comboprint it should be something like this:

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


I would solve this another way, by making combobox1 and combobox2 class variables, like this:

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

This has the advantage that when another function needs to do something with those comboboxes, that they can do that, without you having to pass the comboboxes as parameters to every function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜