Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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

like image 975
aeter Avatar asked Dec 29 '10 11:12

aeter


2 Answers

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]
    ...  
like image 178
mouad Avatar answered Nov 12 '22 03:11

mouad


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.

like image 1
Mew Avatar answered Nov 12 '22 02:11

Mew