如何在 PyGTK 中向 button.connect 添加附加参数?
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
做这样的事情:
在你的回调
comboprint
中它应该是这样的:do something like this:
and in your callback
comboprint
it should be something like this:我会通过另一种方式解决这个问题,通过创建组合框1和组合框2类变量,如下所示:
这样做的优点是,当另一个函数需要对这些组合框执行某些操作时,它们可以做到这一点,而无需将组合框作为参数传递给每个功能。
I would solve this another way, by making combobox1 and combobox2 class variables, like this:
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.