我是新的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)
最好提供最小的测试用例。阅读起来更容易,更容易发现问题,而且更一般化(这也会帮助其他人)。 – gpoo