By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,558 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,558 IT Pros & Developers. It's quick & easy.

How to quit a dialog within a function ?

P: 55
Dear Friends,
I am working in a python based application. I need an option to go
a page with Dialog >> combobox option. But I could not close the dialog box on the onchange event of combobox. My current function is:

Expand|Select|Wrap|Line Numbers
  1.   def mostrar_combobox(self, titulo, texto_etiqueta, lista):
  2.     """
  3.     Mà© All to show a combobox on screen and get the option chosen
  4.     """
  5.     #print texto_etiqueta
  6.     #dialogo = gtk.Dialog(titulo, None, 0, (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
  7.     dialogo = gtk.Dialog(titulo, None, 0, None)
  8.     etiqueta = gtk.Label(texto_etiqueta)
  9.     etiqueta.show()
  10.     dialogo.vbox.pack_start(etiqueta)
  11.     combobox = gtk.combo_box_new_text()
  12.     for x in lista:
  13.       combobox.append_text(x)
  14.     combobox.connect('changed', self.changed_cb)
  15.     #combobox.set_active(0)
  16.     combobox.show()
  17.     dialogo.vbox.pack_start(combobox, False)
  18.     dialogo.run()
  19.     elemento_activo = combobox.get_active()
  20.     dialogo.destroy()
  21.  
  22.     return elemento_activo
  23.  
  24.  
Please advise

Thanks
Anes
Mar 1 '16 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 618
dialogo.destroy() should be in the callback function self.changed_cb (which we don't have so can't comment further).
Mar 1 '16 #2

P: 55
dear dwlabs,
When I give that option in 'changed_cb' as below:
Expand|Select|Wrap|Line Numbers
  1.   def mostrar_combobox(self, titulo, texto_etiqueta, lista):
  2.     """
  3.     Mà© All to show a combobox on screen and get the option chosen
  4.     """
  5.     #print texto_etiqueta
  6.     #dialogo = gtk.Dialog(titulo, None, 0, (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
  7.     dialogo = gtk.Dialog(titulo, None, 0, None)
  8.     etiqueta = gtk.Label(texto_etiqueta)
  9.     etiqueta.show()
  10.     dialogo.vbox.pack_start(etiqueta)
  11.     combobox = gtk.combo_box_new_text()
  12.     for x in lista:
  13.       combobox.append_text(x)
  14.     combobox.connect('changed', self.changed_cb)
  15.     #combobox.set_active(0)
  16.     combobox.show()
  17.     dialogo.vbox.pack_start(combobox, False)
  18.     dialogo.run()
  19.     #print response
  20.     #combobox.connect('changed', self.changed_cb)
  21.     elemento_activo = combobox.get_active()
  22.  
  23.     #dialogo.destroy()
  24.     dialogo.hide()
  25.     return elemento_activo
  26.  
  27.   def changed_cb(self, combobox):
  28.     model = combobox.get_model()
  29.     index = combobox.get_active()
  30.     #self.dialogo.destroy()
  31.     if index:
  32.       self.dialogo.destroy()
  33.       print index
  34.       return index
  35.  
  36.  
got error as

self.dialogo.destroy()
AttributeError: Controlador instance has no attribute 'dialogo'

So need an intelligent solution , waiting for it. In my opinion if I remove that 'changed_cb' def no problem in current working of program . That function is really useless ...

Thanks
Anes
Mar 2 '16 #3

Expert 100+
P: 618
The only thing I can do is point you to a tutorial on classes as there are too many things that you don't understand http://www.freenetpages.co.uk/hp/ala...d/tutclass.htm
Mar 2 '16 #4

P: 55
Dear dwlabs and friends,
I try your code , but still have no solution :(
my code is:

Expand|Select|Wrap|Line Numbers
  1.  def mostrar_combobox(self, titulo, texto_etiqueta, lista):
  2.     """
  3.     Mà© All to show a combobox on screen and get the option chosen
  4.     """
  5.     #print texto_etiqueta
  6.     #dialogo = gtk.Dialog(titulo, None, 0, (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
  7.     dialogo = gtk.Dialog(titulo, None,  gtk.DIALOG_MODAL, None)
  8.     dialogo.connect('response', lambda *x: self.destroy())
  9.     etiqueta = gtk.Label(texto_etiqueta)
  10.     etiqueta.show()
  11.     dialogo.vbox.pack_start(etiqueta)
  12.     combobox = gtk.combo_box_new_text()
  13.     for x in lista:
  14.       combobox.append_text(x)
  15.     combobox.connect('changed', self.changed_cb)
  16.     combobox.show()
  17.     dialogo.vbox.pack_start(combobox, False)
  18.     response = dialogo.run()
  19.     combobox.connect('changed', self.changed_cb)
  20.     dialogo.emit("delete-event", gtk.gdk.Event(gtk.gdk.DELETE))
  21.     elemento_activo = combobox.get_active()
  22.     return elemento_activo
  23.  
  24.  
  25.   def changed_cb(self, combobox):
  26.     #model = combobox.get_model()
  27.     index = combobox.get_active()
  28.     #self.dialogo.destroy()
  29.     #response = self.dialogo.run()
  30.     if index > -1:
  31.       print index
  32.  
  33.       #return index
  34.  
  35.  
  36.  
Do i have use GtkWindow for this purpose ? if so please provide a sample code..



Thanks
Anes
Mar 3 '16 #5

Post your reply

Sign in to post your reply or Sign up for a free account.