PyGTK:从之前的组合框下拉菜单中更改组合框下拉菜单

时间:2012-06-22 22:01:58

标签: python gtk pygtk

我是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)

1 个答案:

答案 0 :(得分: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()
相关问题