如何关闭关于组合框事件更改的GTK对话



我正在使用PyGtk 2.0。在我的程序中,我创建了一个对话框,其中包含一个组合框。对话框没有"确定"或"取消"按钮。选择组合框中的项目时,对话框必须关闭(表示onchange事件)。但是如果没有手动关闭操作,我无法销毁对话框。

我的相关代码是:

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

请告知onchange后如何关闭。

我在这里发布了一个示例代码:http://pastie.org/10748579

但是我无法在我的主应用程序中重现相同的内容。

下面是一个简单的示例,可以执行您想要的操作。我用你的一些代码,我写了几年的代码,以及一些新的东西来构建它。

#!/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()

在 Python 2.6.6, GTK 版本 2.21.3 上测试

最新更新