如何向添加其他参数按钮连接在PyGTK?

2024-10-01 09:38:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把2个ComboBox实例传递给a方法并在那里使用它们(例如,打印它们的活动选择)。我有一些类似的东西:

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”,以便在那里使用它们?使它们成为类域(自我组合框1, 自我组合框2)唯一的办法是?在


Tags: the方法textselfboxgtknewfor
2条回答

我会用另一种方法来解决这个问题,通过创建combbox1和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

这样做的好处是,当另一个函数需要处理这些组合框时,它们可以这样做,而不必将组合框作为参数传递给每个函数。在

做这样的事情:

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

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

^{pr2}$

相关问题 更多 >