Adding items to wxpython combobox...?
Question: I am new to python programming and to this forum as well. I am preparing a
wxpython program (GUI) which has 2 comboboxes. First is for the Teams and the
second for team members. All the data is stored into a access database and I
am successful in fetching and adding the teams list to the teams combobox.

Now I want the team member combobox to load on the text change event of team
combobox. for this I bind it using the EVT_TEXT method and called a self
defined method to first take the team name from the team combobox and then run
the query and load the relevant team members to the second combobox. to much
extent I am successful but the team members are getting added to the team
combobox itself.

Please have a look at the below code first:

    
    
    import wx, pyodbc
    class MyFrame(wx.Frame):
        def __init__(self, Parent, Title):
            super(MyFrame, self).__init__(None, title=Title, size=(400,400))
            #creating the panel in which all widgets will be stored/created.
            #self.panel1 = wx.Panel(self, pos=(1,1),size=(382,100),style=wx.RAISED_BORDER)
            #now creating the first Label inside the panel
    
            a = teamData()
            #rows = a.runQueryEmpList("Mama Badi")
            TmLst = a.runQueryTmList()
            abc=[]
            for r in TmLst:
                abc.append(r.Team_Name)
    
            #static box for the employee details
            self.myvbfrm = wx.StaticBox(self,-1,label="Employee Detail:-",pos=(1,1),size=(380,98))
            #items for the employee details
            LblName = wx.StaticText(self.myvbfrm,-1, 'Team Name:-',(8,20))
            LblName2 = wx.StaticText(self.myvbfrm,-1, 'Employee Name:-',(8,50))
            # team combobox
            TeamList = myComboBox(self.myvbfrm,(200,20),(100,50))
            TeamList.addItem(abc)
            # employee combobox
            #EmpList = wx.ComboBox(self.myvbfrm,-1,"",(200,50),(100,50))
            EmpList = myComboBox(self.myvbfrm,(200,50),(100,50))
    
            TeamList.Bind(wx.EVT_TEXT, TeamList.addTeamMember)
    
            #creating the panel in which all widgets will be stored/created.
            self.panel2 = wx.Panel(self, pos=(10,175),size=(365,150),style=wx.RAISED_BORDER)
            #now creating the first Label inside the panel
    
            myFont = wx.Font(8,wx.DECORATIVE, wx.NORMAL, wx.BOLD,True)
            myLblFont = wx.Font(12,wx.DECORATIVE, wx.NORMAL, wx.BOLD,True)
    
            #Lbl.SetFont(myFont)
            #Lbl.SetForegroundColour((0,0,0))
            #Lbl.SetBackgroundColour((204,204,204))
            self.SetBackgroundColour((237,237,237))
    
            LblName.SetFont(myLblFont)
            LblName2.SetFont(myLblFont)
    
            # now under this __init__ method i will also initiate the method which
            # will create the MenuBar and the Menu Items.
            self.AddMenu()
    
        # now i will also have to create a method named as AddMenu so that
        # can be run (which will add the Menu Items.
        def AddMenu(self):
            myMenuBar = wx.MenuBar()
            myFileMenu = wx.Menu()
            myEditMenu = wx.Menu()
            exitBtn = myFileMenu.Append(wx.ID_EXIT, 'Exit App')
            editBtn = myEditMenu.Append(wx.ID_MOVE_FRAME, 'Move App')
            myMenuBar.Append(myFileMenu, '&File')
            myMenuBar.Append(myEditMenu, '&Edit')
            self.SetMenuBar(myMenuBar)
    
            self.Centre()
            self.Show()
    
    class teamData():
    
        def runQueryEmpList(self,Tname):
            self.Tname = Tname
            # set up some constants
            myDb = 'D:\\Python projects\\Python programs\\trial.accdb'
            DRV = '{Microsoft Access Driver (*.mdb)}'
            PWD = 'pw'
            # connect to db
            conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s' % (myDb))
            cur = conn.cursor()
            # run a query and get the results 
            SQL = 'SELECT Emp_Name FROM Table1 WHERE Team = ?'
            return cur.execute(SQL, self.Tname).fetchall()
            cur.close()
            conn.close()
    
        def runQueryTmList(self):
            # set up some constants
            myDb = 'D:\\Python projects\\Python programs\\trial.accdb'
            DRV = '{Microsoft Access Driver (*.mdb)}'
            PWD = 'pw'
            # connect to db
            conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s' % (myDb))
            cur = conn.cursor()
            # run a query and get the results 
            SQL = 'SELECT Team_Name FROM Teams'
            return cur.execute(SQL).fetchall()
            cur.close()
            conn.close()
    
    class myComboBox(wx.ComboBox):
        def __init__(self, parent, lstposition, lstsize):
            super(myComboBox, self).__init__(parent, -1, value="", pos=lstposition, size=lstsize)
    
        def addItem(self, Lst=[]):
            self.Lst = Lst
            for el in self.Lst:
                self.Append(el)
    
        def addTeamMember(self,extra):
            self.extra = extra
            a = teamData()
            rows = a.runQueryEmpList(self.GetValue())
            Emp_List=[]
            for r in rows:
                self.Append(r.Emp_Name)
    
    
    
    class myFrm(wx.StaticBox):
        def __init__(self, parent, lblstring, position, BxSize):
            super(myFrm, self).__init__(parent,-1,label=lblstring,pos=position,size=BxSize)
    
        def borderColor(self):
            self
    
    
    
    app = wx.App()
    frame = MyFrame(None, 'Clysdale Activity Tracker')
    app.MainLoop()
    

I understand why they are getting added to the team combobox (because I am
Append'ing the items to self). How should I refer to the other combobox in
`addTeamMember()` method?


Answer: Normally, you would bind to a handler in your frame or panel and have the
comboboxes defined as class variables:

    
    
    self.TeamList = myComboBox(self.myvbfrm,(200,20),(100,50))
    self.TeamList.Bind(wx.EVT_TEXT, self.onUpdate)
    self.EmpList = myComboBox(self.myvbfrm,(200,50),(100,50))
    

Then you could just append items in the handler:

    
    
    def onUpdate(self, event):
        team = self.TeamList.GetValue()
        if team == "Tigers":
            self.EmpList.append(some_list)
    

If you want to keep your way of doing things, then create the EmpList object
first and pass it into the myComboBox class as another parameter when you
create TeamList. Then you can append to it in the TeamList instance. Something
like the following should work:

    
    
    import wx, pyodbc
    class MyFrame(wx.Frame):
        def __init__(self, Parent, Title):
            super(MyFrame, self).__init__(None, title=Title, size=(400,400))
            #creating the panel in which all widgets will be stored/created.
            #self.panel1 = wx.Panel(self, pos=(1,1),size=(382,100),style=wx.RAISED_BORDER)
            #now creating the first Label inside the panel
    
            a = teamData()
            #rows = a.runQueryEmpList("Mama Badi")
            TmLst = a.runQueryTmList()
            abc=[]
            for r in TmLst:
                abc.append(r.Team_Name)
    
            #static box for the employee details
            self.myvbfrm = wx.StaticBox(self,-1,label="Employee Detail:-",pos=(1,1),size=(380,98))
            #items for the employee details
            LblName = wx.StaticText(self.myvbfrm,-1, 'Team Name:-',(8,20))
            LblName2 = wx.StaticText(self.myvbfrm,-1, 'Employee Name:-',(8,50))
            # employee combobox
            #EmpList = wx.ComboBox(self.myvbfrm,-1,"",(200,50),(100,50))
            EmpList = EmpComboBox(self.myvbfrm,(200,50),(100,50))
    
            # team combobox
            TeamList = TeamComboBox(self.myvbfrm,(200,20),(100,50), EmpList)
            TeamList.addItem(abc)
            TeamList.Bind(wx.EVT_TEXT, TeamList.addTeamMember)
    
            #creating the panel in which all widgets will be stored/created.
            self.panel2 = wx.Panel(self, pos=(10,175),size=(365,150),style=wx.RAISED_BORDER)
            #now creating the first Label inside the panel
    
            myFont = wx.Font(8,wx.DECORATIVE, wx.NORMAL, wx.BOLD,True)
            myLblFont = wx.Font(12,wx.DECORATIVE, wx.NORMAL, wx.BOLD,True)
    
            #Lbl.SetFont(myFont)
            #Lbl.SetForegroundColour((0,0,0))
            #Lbl.SetBackgroundColour((204,204,204))
            self.SetBackgroundColour((237,237,237))
    
            LblName.SetFont(myLblFont)
            LblName2.SetFont(myLblFont)
    
            # now under this __init__ method i will also initiate the method which
            # will create the MenuBar and the Menu Items.
            self.AddMenu()
    
        # now i will also have to create a method named as AddMenu so that
        # can be run (which will add the Menu Items.
        def AddMenu(self):
            myMenuBar = wx.MenuBar()
            myFileMenu = wx.Menu()
            myEditMenu = wx.Menu()
            exitBtn = myFileMenu.Append(wx.ID_EXIT, 'Exit App')
            editBtn = myEditMenu.Append(wx.ID_MOVE_FRAME, 'Move App')
            myMenuBar.Append(myFileMenu, '&File')
            myMenuBar.Append(myEditMenu, '&Edit')
            self.SetMenuBar(myMenuBar)
    
            self.Centre()
            self.Show()
    
    class myComboBox(wx.ComboBox):
        def __init__(self, parent, lstposition, lstsize):
            super(myComboBox, self).__init__(parent, -1, value="", pos=lstposition, size=lstsize)
    
        def addItem(self, Lst=[]):
            self.Lst = Lst
            for el in self.Lst:
                self.Append(el)
    
        def addTeamMember(self,extra):
            raise NotImplementedError
    
    ########################################################################
    class EmpComboBox(myComboBox):
        """"""
        pass
    
    ########################################################################
    class TeamComboBox(myComboBox):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self, parent, lstposition, lstsize, empComboBox=None):
            """Constructor"""
            super(myComboBox, self).__init__(parent, -1, value="", pos=lstposition, size=lstsize)
            self.empComboBox = empComboBox
    
    
        def addTeamMember(self,extra):
            self.extra = extra
            a = teamData()
            rows = a.runQueryEmpList(self.GetValue()) 
            for r in rows:
                self.empComboBox.Append(r.Emp_Name)
    
    
    class myFrm(wx.StaticBox):
        def __init__(self, parent, lblstring, position, BxSize):
            super(myFrm, self).__init__(parent,-1,label=lblstring,pos=position,size=BxSize)
    
        def borderColor(self):
            self
    
    
    
    app = wx.App()
    frame = MyFrame(None, 'Clysdale Activity Tracker')
    app.MainLoop()
    

Basically you'll want to subclass your ComboBox class and override the
addTeamMember method. Since I couldn't run your code, I wasn't able to test
the example above, but I believe it will work (although it might need a tweak
here or there).

