开发者

Python GTK adding signal to a combo box

I create a combo box using PyGTK:

fileAttrCombo = gtk.ComboBox(); 

I want to attach a signal开发者_如何学JAVA handler for this combo box. This signal handler handles when user change selection in the combo box.

What is be the best approach to do this ?


The combobox has a "changed" 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()


try to replace "if index:" by "if index != None:" for getting the 1st value of combobox which has index equal to 0

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜