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

发布于 2024-10-09 21:35:55 字数 611 浏览 5 评论 0原文

我想将 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.

如何将组合框1和组合框2传递给方法“comboprint”,以便我可以在那里使用它们?使它们成为类字段(self.combobox1,self.combobox2)是执行此操作的唯一方法吗?

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 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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二货你真萌 2024-10-16 21:35:55

做这样的事情:

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

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

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

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]
    ...  
德意的啸 2024-10-16 21:35:55

我会通过另一种方式解决这个问题,通过创建组合框1和组合框2类变量,如下所示:

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文