通过选择combobox1 wxpython动态更新组合框2

时间:2016-07-01 22:09:34

标签: python-2.7 user-interface wxpython

我是python的新手并尝试学习一些基本代码。我正在做一些项目,下面有演示代码。

我在这段代码中唯一无法改变的是 __ init __ def update()部分,因为它们也链接到其他一些代码。

问题是当我选择值为' 2'在combobox-1上,我得到的值是' B100,B200,B300'在combobox-2中但如果我选择值' 1'在选择值' 2'之后,在combobox-1上,我没有在我的combobox-2上获得任何更新列表。如果需要,在IDE上复制并粘贴代码。

#!/usr/bin/python
#20_combobox.py

import wx
import wx.lib.inspection

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
    wx.Frame.__init__(self, *args, **kwargs)

        self.p1 = wx.Panel(self)
        lst = ['1','2','3']
        self.st = wx.ComboBox(self.p1, -1, choices = self.lst1(), style=wx.TE_PROCESS_ENTER)
        self.st2 = wx.ComboBox(self.p1, -1, choices = self.lst2(), style=wx.TE_PROCESS_ENTER)

        self.st.Bind(wx.EVT_COMBOBOX, self.update)
        self.st2.Bind(wx.EVT_COMBOBOX, self.update)


    def lst1(self):
        lst=['1','2','3']
        return lst

    def lst2(self):
        lst =['A100','A200','A300','B100','B200','B300','C100','C200','C300','D100','D200','D300']
        dummyvar=[]
        dummyvar1=[]

        if self.st.GetStringSelection() == '1':
            self.st2.Clear()
            for number in lst:
                if number.startswith('A'):
                # if number.startswith('A'):
                    self.st2.Append(number)
                    self.st.Hide()

        elif self.st.GetStringSelection() == '2':
            self.st2.Clear()
            for number in lst:
                if number.startswith('B'):
                    self.st2.Append(number)



        return lst


    def update(self, event):
        self.lst1a=self.lst1()
        self.lst2a=self.lst2()


class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, '20_combobox.py')
        frame.Show()
        self.SetTopWindow(frame)
        return 1

if __name__ == "__main__":
    app = MyApp(0)
#    wx.lib.inspection.InspectionTool().Show()
    app.MainLoop()

1 个答案:

答案 0 :(得分:0)

不要混淆对象属性和方法。 self.list1 != self.list1(),Python试图通过在第二次调用self.update时引发错误来告诉您的事实。查看更明确的关注点分离的示例:

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.p1 = wx.Panel(self)
        self.lst1 = ['1','2','3']
        self.lst2 = self.get_lst2()

        self.st = wx.ComboBox(self.p1, -1, choices = self.lst1, style=wx.TE_PROCESS_ENTER)
        self.st2 = wx.ComboBox(self.p1, -1, choices = self.lst2, style=wx.TE_PROCESS_ENTER, pos=(-1,100), size=(100,-1))

        self.st.Bind(wx.EVT_COMBOBOX, self.update)

    def get_lst2(self, selectn=None):
        lst =['A100','A200','A300','B100','B200','B300','C100','C200','C300','D100','D200','D300']

        ret =  []
        if selectn == '1':
            for entry in lst:
                if entry.startswith('A'):
                    ret.append(entry)
        elif selectn == '2':
            for entry in lst:
                if entry.startswith('B'):
                    ret.append(entry)
        else:
            ret = lst
        return ret

    def update(self, event):
        selectn = self.st.GetStringSelection()
        self.lst2 = self.get_lst2(selectn)
        self.st2.Clear()
        for number in self.lst2:
            self.st2.Append(number)
        if selectn == '1':
            self.st.Hide()