2010-04-24 48 views
0

我創建使用PyGTK的組合框:Python的GTK將信號組合框

fileAttrCombo = gtk.ComboBox(); 

我要附加的信號處理程序,該組合框。該信號處理程序在用戶更改組合框中的選擇時處理。

什麼是最好的方法來做到這一點?

回答

2

組合框有一個「改變」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() 
+0

謝謝這就像一個魅力=) – zfranciscus 2010-04-24 23:04:31

0

嘗試更換「如果索引:」通過「如果指數=無:」用於獲取組合框的第一個值,其中有指數等於0