How to quit a dialog within a function ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amskape
    New Member
    • Apr 2010
    • 56

    How to quit a dialog within a function ?

    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:

    Code:
      def mostrar_combobox(self, titulo, texto_etiqueta, lista):
        """
        MÃ © All to show a combobox on screen and get the option chosen
        """
        #print texto_etiqueta
        #dialogo = gtk.Dialog(titulo, None, 0, (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
        dialogo = gtk.Dialog(titulo, None, 0, None)
        etiqueta = gtk.Label(texto_etiqueta)
        etiqueta.show()
        dialogo.vbox.pack_start(etiqueta)
        combobox = gtk.combo_box_new_text()
        for x in lista:
          combobox.append_text(x)
        combobox.connect('changed', self.changed_cb)
        #combobox.set_active(0)
        combobox.show()
        dialogo.vbox.pack_start(combobox, False)
        dialogo.run()
        elemento_activo = combobox.get_active()
        dialogo.destroy()
        
        return elemento_activo
    Please advise

    Thanks
    Anes
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    dialogo.destroy () should be in the callback function self.changed_cb (which we don't have so can't comment further).

    Comment

    • amskape
      New Member
      • Apr 2010
      • 56

      #3
      dear dwlabs,
      When I give that option in 'changed_cb' as below:
      Code:
        def mostrar_combobox(self, titulo, texto_etiqueta, lista):
          """
          MÃ © All to show a combobox on screen and get the option chosen
          """
          #print texto_etiqueta
          #dialogo = gtk.Dialog(titulo, None, 0, (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
          dialogo = gtk.Dialog(titulo, None, 0, None)
          etiqueta = gtk.Label(texto_etiqueta)
          etiqueta.show()
          dialogo.vbox.pack_start(etiqueta)
          combobox = gtk.combo_box_new_text()
          for x in lista:
            combobox.append_text(x)
          combobox.connect('changed', self.changed_cb)
          #combobox.set_active(0)
          combobox.show()
          dialogo.vbox.pack_start(combobox, False)
          dialogo.run()
          #print response
          #combobox.connect('changed', self.changed_cb)
          elemento_activo = combobox.get_active()
          
          #dialogo.destroy()
          dialogo.hide()
          return elemento_activo
       
        def changed_cb(self, combobox):
          model = combobox.get_model()
          index = combobox.get_active()
          #self.dialogo.destroy()
          if index:
            self.dialogo.destroy()
            print index
            return index
      got error as

      self.dialogo.de stroy()
      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

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        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

        Comment

        • amskape
          New Member
          • Apr 2010
          • 56

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

          Code:
           def mostrar_combobox(self, titulo, texto_etiqueta, lista):
              """
              MÃ © All to show a combobox on screen and get the option chosen
              """
              #print texto_etiqueta
              #dialogo = gtk.Dialog(titulo, None, 0, (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
              dialogo = gtk.Dialog(titulo, None,  gtk.DIALOG_MODAL, None)
              dialogo.connect('response', lambda *x: self.destroy())
              etiqueta = gtk.Label(texto_etiqueta)
              etiqueta.show()
              dialogo.vbox.pack_start(etiqueta)
              combobox = gtk.combo_box_new_text()
              for x in lista:
                combobox.append_text(x)
              combobox.connect('changed', self.changed_cb)
              combobox.show()
              dialogo.vbox.pack_start(combobox, False)
              response = dialogo.run()
              combobox.connect('changed', self.changed_cb)
              dialogo.emit("delete-event", gtk.gdk.Event(gtk.gdk.DELETE))
              elemento_activo = combobox.get_active()
              return elemento_activo
              
           
            def changed_cb(self, combobox):
              #model = combobox.get_model()
              index = combobox.get_active()
              #self.dialogo.destroy()
              #response = self.dialogo.run()
              if index > -1:
                print index
              
                #return index
          Do i have use GtkWindow for this purpose ? if so please provide a sample code..



          Thanks
          Anes
          Last edited by amskape; Mar 3 '16, 11:18 AM. Reason: Image not came

          Comment

          Working...