2012-06-22 57 views
0

我是新的python和Pygtk,我不知道如何更新从前面的下拉菜单项下拉菜单项。我试图设置一个组合框(cb2)的列表库,它取决于前一个组合框(cb1)的选择。所以我认为这是一个动态的cb2,因为它取决于cb1用户的选择。PyGTK:从以前的组合框下拉菜单中更改组合框下拉菜单

cb1有项目C1和C2。如果我选择C1,我希望cb2项目是2,3和4.如果我选择C2,我希望cb2项目是2,3,4和6.

有人可以帮助我吗?

这是我迄今为止的测试用例代码。但更改不起作用!

import pygtk 
pygtk.require("2.0") 
import gtk 

class test: 
    def __init__(self): 
     self.window = gtk.Window(type=gtk.WINDOW_TOPLEVEL) 
     self.window.connect("destroy", lambda w: gtk.main_quit()) 

     self.vbox_setup = gtk.VBox(False, 0) 
     self.vbox_setup.set_border_width(10) 
     self.window.add(self.vbox_setup) 
     self.conf_1() 

     self.window.show_all() 
     gtk.main() 

    def conf_1(self): 
     self.obs_box = gtk.VBox(False, 0) 
     self.vbox_setup.pack_start(self.obs_box, False, True, 0) 
     self.label = gtk.Label('Conf1') 
     self.obs_box.pack_start(self.label, False, True, 3) 
     self.label_lat = gtk.Label('Latitude: ?') 
     self.combobox_1 = gtk.ComboBox() 
     self.liststore = gtk.ListStore(str) 
     self.cell = gtk.CellRendererText() 
     self.combobox_1.pack_start(self.cell) 
     self.combobox_1.add_attribute(self.cell, 'text', 0) 
     self.obs_box.pack_start(self.combobox_1, False, True, 3) 
     self.liststore.append(['Select:']) 
     self.liststore.append(['C1']) 
     self.liststore.append(['C2']) 
     self.combobox_1.set_model(self.liststore) 
     self.combobox_1.connect('changed', self.conf1_choice) 
     self.combobox_1.connect('changed', self.conf2_choice) 
     self.combobox_1.set_active(0) 
     self.obs_box.pack_start(self.label_lat, False, True, 3) 

    def conf1_choice(self, combobox): 
     model = combobox.get_model() 
     index = combobox.get_active() 
     if model[index][0] == 'C1': 
      self.latitude = -4.23 
     elif model[index][0] == 'C2': 
      self.latitude = 45.22 
     else : 
      self.latitude = 0 
     lat_dgr = int(self.latitude) 
     lat_mn = int((self.latitude %1)*60.) 
     lat_s = ((self.latitude %1)*60. %1)*60. 
     self.label_lat.set_label('Latitude: '+str(lat_dgr)+u'\u00B0 '+ 
          str(lat_mn)+u'\u0027 '+str(round(lat_s,2))+u'\u0027\u0027') 

    def conf2_choice(self, widget): 
     self.configuration_box = gtk.VBox(False, 0) 
     self.vbox_setup.pack_start(self.configuration_box, False, True, 0) 
     self.label = gtk.Label('Configuration') 
     self.configuration_box.pack_start(self.label, False, True, 3) 
     self.combobox_2 = gtk.ComboBox() 
     self.liststore = gtk.ListStore(str) 
     self.cell = gtk.CellRendererText() 
     self.combobox_2.pack_start(self.cell) 
     self.combobox_2.add_attribute(self.cell, 'text', 0) 
     self.configuration_box.pack_start(self.combobox_2, False, True, 3) 
     self.liststore.append(['Select:']) 
     model = self.combobox_1.get_model() 
     index = self.combobox_1.get_active() 
     if model[index][0] == 'C1': 
      self.liststore.append(['2']) 
      self.liststore.append(['3']) 
      self.liststore.append(['4']) 
     if model[index][0] == 'C2': 
      self.liststore.append(['2']) 
      self.liststore.append(['3']) 
      self.liststore.append(['4']) 
      self.liststore.append(['6']) 
     self.combobox_2.set_model(self.liststore) 
     self.combobox_2.set_active(0) 
     self.combobox_2.connect('changed', self.conf3_choice) 

    def conf3_choice(self, widget): 
     pass 

if __name__ == "__main__": 
    uv = test() 

此外,我还希望的是,选择CB1 = C1和CB2 = 2创建其他2个组合框的项目T1,T2和T3,或者如果我选择CB1 = C1和CB2 = 4,它将创建4与项目T1,T2和T3的其他组合框。对于cb1 = C2,但对于项目U1,U2,U3和U4也是如此。在这种情况下,* conf3_choice *的实现为:

def conf3_choice(self, widget): 
    model_1 = self.combobox_1.get_model() 
    index_1 = self.combobox_1.get_active() 
    model_2 = self.combobox_2.get_model() 
    index_2 = self.combobox_2.get_active() 
    self.new_box_1 = gtk.VBox(False, 0) 
    self.vbox_setup.pack_start(self.new_box_1, False, True, 0) 
    self.new_box_2 = gtk.VBox(False, 0) 
    self.vbox_setup.pack_start(self.new_box_2, False, True, 0) 
    self.combobox_3 = gtk.ComboBox() 
    self.liststore = gtk.ListStore(str) 
    self.cell = gtk.CellRendererText() 
    self.combobox_3.pack_start(self.cell) 
    self.combobox_3.add_attribute(self.cell, 'text', 0) 
    self.liststore.append(['Select:']) 
    if model_1[index][0]=='C1': 
     self.liststore.append(['T1']) 
     self.liststore.append(['T2']) 
     self.liststore.append(['T3']) 
     self.combobox_3.set_model(self.liststore) 
     self.combobox_3.set_active(0) 
     if model_2[index][0]=='2': 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
     if model_2[index][0]=='3': 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
     if model_2[index][0]=='4': 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
    if model_1[index][0]=='C2': 
     self.liststore.append(['U1']) 
     self.liststore.append(['U2']) 
     self.liststore.append(['U3']) 
     self.liststore.append(['U4']) 
     self.combobox_3.set_model(self.liststore) 
     self.combobox_3.set_active(0) 
     if model_2[index][0]=='2': 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
     if model_2[index][0]=='3': 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
     if model_2[index][0]=='4': 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
     if model_2[index][0]=='6': 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
      self.configuration_box.pack_start(self.combobox_3, False, True, 3) 
+0

最好提供最小的测试用例。阅读起来更容易,更容易发现问题,而且更一般化(这也会帮助其他人)。 – gpoo

回答

0

您正在与每个组合框共享相同的模型(self.liststore)。与self.cell类似。你应该为它们使用局部变量,如果你以后需要它们,你可以用get_model()等等。其次,每次组合框更改时创建小部件似乎不是一个好主意;更好的方法是在开始时创建小部件,并根据需要启用/禁用它们。在所有情况下,您只需更新型号(liststore)。

为了将相关功能放在一起,我重新安排了一些代码。通过使用局部变量和正确的顺序,您现在会注意到代码正常工作。您可以在此推断代码,使第3个和第4个组合框工作。

最后但并非最不重要,有必要清除()第二个组合框的模型,您可以随时添加项目。

import pygtk 
pygtk.require("2.0") 
import gtk 

class test: 
    def __init__(self): 
     self.window = gtk.Window(type=gtk.WINDOW_TOPLEVEL) 
     self.window.connect("destroy", lambda w: gtk.main_quit()) 

     self.vbox_setup = gtk.VBox(False, 0) 
     self.vbox_setup.set_border_width(10) 
     self.window.add(self.vbox_setup) 
     self.create_comboxbox() 

     self.window.show_all() 
     gtk.main() 

    def create_comboxbox(self): 
     # Main Combo box 
     liststore = gtk.ListStore(str) 
     liststore.append(['Select:']) 
     liststore.append(['C1']) 
     liststore.append(['C2']) 

     self.obs_box = gtk.VBox(False, 0) 
     self.vbox_setup.pack_start(self.obs_box, False, True, 0) 
     label = gtk.Label('Conf1') 
     self.label_lat = gtk.Label('Latitude: ?') 

     self.combobox_1 = gtk.ComboBox() 
     cell = gtk.CellRendererText() 
     self.combobox_1.pack_start(cell) 
     self.combobox_1.add_attribute(cell, 'text', 0) 
     self.combobox_1.set_model(liststore) 
     self.combobox_1.set_active(0) 
     self.combobox_1.connect('changed', self.conf1_choice) 
     self.combobox_1.connect('changed', self.conf2_choice) 

     self.obs_box.pack_start(label, False, True, 3) 
     self.obs_box.pack_start(self.combobox_1, False, True, 3) 
     self.obs_box.pack_start(self.label_lat, False, True, 3) 

     # Secondary Combo box 
     liststore = gtk.ListStore(str) 
     liststore.append(['Select:']) 

     label = gtk.Label('Configuration') 
     self.combobox_2 = gtk.ComboBox() 

     cell = gtk.CellRendererText() 
     self.combobox_2.pack_start(cell) 
     self.combobox_2.add_attribute(cell, 'text', 0) 
     self.combobox_2.set_model(liststore) 
     self.combobox_2.set_active(0) 
     #self.combobox_2.connect('changed', self.conf3_choice) 

     self.obs_box.pack_start(label, False, True, 3) 
     self.obs_box.pack_start(self.combobox_2, False, True, 3) 

    def conf1_choice(self, combobox): 
     model = combobox.get_model() 
     index = combobox.get_active() 
     if model[index][0] == 'C1': 
      self.latitude = -4.23 
     elif model[index][0] == 'C2': 
      self.latitude = 45.22 
     else: 
      self.latitude = 0 
     lat_dgr = int(self.latitude) 
     lat_mn = int((self.latitude %1)*60.) 
     lat_s = ((self.latitude %1)*60. %1)*60. 
     self.label_lat.set_label('Latitude: '+str(lat_dgr)+u'\u00B0 '+ 
          str(lat_mn)+u'\u0027 '+str(round(lat_s,2))+u'\u0027\u0027') 

    def conf2_choice(self, widget): 
     model = self.combobox_1.get_model() 
     index = self.combobox_1.get_active() 
     liststore = self.combobox_2.get_model() 
     liststore.clear() 
     liststore.append(['Select:']) 
     if model[index][0] == 'C1': 
      liststore.append(['2']) 
      liststore.append(['3']) 
      liststore.append(['4']) 
     if model[index][0] == 'C2': 
      liststore.append(['2']) 
      liststore.append(['3']) 
      liststore.append(['4']) 
      liststore.append(['6']) 
     self.combobox_2.set_model(liststore) 
     self.combobox_2.set_active(0) 

if __name__ == "__main__": 
    uv = test()