1

I am using PyGtk 2.0. In my program I created a Dialog box which includes a Combobox. The Dialog box has no OK or Cancel button. The Dialog must close when the item in the Combobox is selected (means in onchange event). But I could not destroy the Dialog without a manual close operation.

My relevant code is:

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)
    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)
    response = dialogo.run()

    elemento_activo = combobox.get_active()

    return elemento_activo

    dialogo.hide()

def changed_cb(self, combobox):
    index = combobox.get_active()
    if index > -1:
        print index

Please advise how it can close on after onchange.

I got a sample code posted here: http://pastie.org/10748579

But I could not reproduce same in my main app.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
Anes
  • 35
  • 1
  • 9
  • 2
    Anes, I've noticed that with multiple questions in 5 years you haven't accepted any answers. I wanted you to know that as asker you have the option (and responsibility) to mark answers that solve your question as accepted. You can do so by clicking the tick mark to the left of each answer, and doing so will give reputation both to you and to the asker. Upvoting and accepting answers are the ways you can thank those who help you (upvoting good answers, and accepting answers that solve your problem). – Andras Deak -- Слава Україні Mar 06 '16 at 20:25

1 Answers1

2

Here's a simple example that does what you want. I built it using some of your code, some code I wrote a few years, and some new stuff.

#!/usr/bin/env python

''' Create a GTK Dialog containing a combobox that closes 
    when a combobox item is selected

    See http://stackoverflow.com/q/35812198/4014959

    Written by PM 2Ring 2016.03.05
'''

import pygtk
pygtk.require('2.0')
import gtk

lista = ('zero', 'one', 'two', 'three')

class Demo:
    def __init__(self):
        self.win = win = gtk.Window(gtk.WINDOW_TOPLEVEL)
        win.connect("destroy", lambda w: gtk.main_quit())

        button = gtk.Button("Open dialog")
        button.connect("clicked", self.dialog_button_cb)
        win.add(button)
        button.show()

        self.dialog = gtk.Dialog("Combo dialog", self.win,
            gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
            (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT))

        combobox = gtk.combo_box_new_text()
        for s in lista:
            combobox.append_text(s)
        combobox.connect("changed", self.combo_cb)
        self.dialog.action_area.pack_end(combobox)
        combobox.show()

        win.show()

    def dialog_button_cb(self, widget):
        response = self.dialog.run()
        print "dialog response:", response
        self.dialog.hide()
        return True

    def combo_cb(self, combobox):
        index = combobox.get_active()
        if index > -1:
            print "combo", index, lista[index]
            self.dialog.response(gtk.RESPONSE_ACCEPT)
        return True

def main():
    Demo()
    gtk.main()


if __name__ == "__main__":
    main()

Tested on Python 2.6.6, GTK version 2.21.3

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182