Я новичок в python и Pygtk, и я не знаю, как обновлять выпадающие пункты меню из предыдущих выпадающих меню. Я пытаюсь установить liststore combobox (cb2), который зависит от выбора предыдущего combobox (cb1). Поэтому я считаю, что это динамический cb2, поскольку он зависит от выбора пользователя cb1.PyGTK: Измените выпадающее меню combobox из предыдущего выпадающего меню combobox
В 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