我是 python 和 Pygtk 的新手,我不知道如何从以前的下拉菜单项更新下拉菜单项。我正在尝试设置组合框(cb2)的列表存储,这取决于先前组合框(cb1)的选择。所以我认为这是一个动态的 cb2,因为它取决于 cb1 用户的选择。
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)