gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:51:26 24 4
gpt4 key购买 nike

我正在使用 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 上测试

关于python - 如何关闭组合框事件更改时的 GTK 对话?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35812198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com