【发布时间】:2012-09-21 05:08:24
【问题描述】:
我使用 PyGTK 创建了一个组合框:
fileAttrCombo = gtk.ComboBox();
我想为这个组合框附加一个信号处理程序。当用户在组合框中更改选择时,此信号处理程序会进行处理。
最好的方法是什么?
【问题讨论】:
标签: python
我使用 PyGTK 创建了一个组合框:
fileAttrCombo = gtk.ComboBox();
我想为这个组合框附加一个信号处理程序。当用户在组合框中更改选择时,此信号处理程序会进行处理。
最好的方法是什么?
【问题讨论】:
标签: python
组合框有一个“更改”signal。
This is a nice minimal example of using it.
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
class ComboBoxExample:
def __init__(self):
window = gtk.Window()
window.connect('destroy', lambda w: gtk.main_quit())
combobox = gtk.combo_box_new_text()
window.add(combobox)
combobox.append_text('Select a pie:')
combobox.append_text('Apple')
combobox.append_text('Cherry')
combobox.append_text('Blueberry')
combobox.append_text('Grape')
combobox.append_text('Peach')
combobox.append_text('Raisin')
combobox.connect('changed', self.changed_cb)
combobox.set_active(0)
window.show_all()
return
def changed_cb(self, combobox):
model = combobox.get_model()
index = combobox.get_active()
if index:
print 'I like', model[index][0], 'pie'
return
def main():
gtk.main()
return
if __name__ == "__main__":
bcb = ComboBoxExample()
main()
【讨论】:
尝试将“if index:”替换为“if index!= None:”以获得索引等于 0 的组合框的第一个值
【讨论】: