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

2024-05-06 17:32:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是pygtk2.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

但我无法在我的主应用程序中复制相同的内容。在


Tags: selfnonegtkgetindexshow对话框active
1条回答
网友
1楼 · 发布于 2024-05-06 17:32:57

下面是一个简单的例子,它可以满足您的需要。我用你的一些代码,我写了几年的代码,还有一些新的东西。在

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

在Python2.6.6、GTK版本2.21.3上测试

相关问题 更多 >