[pygtk] Problem passing data to a callback

Doug Quale quale1 at charter.net
Mon Aug 30 22:59:25 WST 2004

Bruno Dusausoy <bdusauso at beeznest.net> writes:

> I have a tiny problem with a callback.
> Here's the code:
> def clientAdd (self, button, *args):
> 	addclient = gtk.Dialog ("Ajouter un client",
> 		self.wtree.get_widget("mainwindow"),
> 	addclient.set_border_width(5)
> 	addclient.set_has_separator (gtk.FALSE)
> 	label = gtk.Label ()
> 	label.set_markup ("""<b>Veuillez entrer un nom\n pour le
> 				client</b>""")
> 	addclient.vbox.pack_start(label)
> 	entry = gtk.Entry ()
> 	addclient.vbox.pack_start(entry)
> 	addclient.connect ("response", self.onClientAddResponse,
> 			entry.get_text())
> 	addclient.show_all()

When you connect to the "response" signal, the call entry.get_text()
is executed immediately.  At this point the entry is empty, so

 	addclient.connect ("response", self.onClientAddResponse,

is exactly the same as

 	addclient.connect ("response", self.onClientAddResponse,

and the data passed to the callback is always the empty string.

You want the get_text() call to be made in the callback.  The easiest
way to do this is to pass the entry itself to the callback

 	addclient.connect ("response", self.onClientAddResponse, entry)

and then change your callback function to get the entry text itself

        def onClientAddResponse (self, dialog, response, entry):
            data = entry.get_text()
 	    if response == gtk.RESPONSE_ACCEPT:
 		print "Company name added : ", data
 	    elif response == gtk.RESPONSE_REJECT:
 		dialog.destroy ()

More information about the pygtk mailing list