Search code examples
pythonwxpythonwxwidgets

Dynamically populating widgets, how to then access them?


So, I've got a small project here that searches a path for files (*.db) and then makes a checkbox and a text control for those widgets. This part works just fine when I run the app:

    # Get a count of *.db from the filesystem
    numDB = scrubDB(os.getcwd())

    # Checkbox (enable, disable for launch)
    # textCtrl (for Proxy name in controller)
    # database name (based on *.db)
    for db in numDB:
        check = wx.CheckBox(self, -1, db)
        sizer.Add(check, pos=(xIndex,0), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=10)
        label = wx.StaticText(panel, label="")
        sizer.Add(label, pos=(xIndex,1), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=10)
        name = wx.TextCtrl(panel)
        #Set Temp Name
        if db.endswith('.db'):
            name.Value = db[:-3]
        sizer.Add(name, pos=(xIndex,2), span=(1,3),flag=wx.EXPAND|wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.TOP, border=5)
        xIndex +=1
    #-------------------------------------------------------


    sizer.AddGrowableCol(2)
    panel.SetSizer(sizer)

This would ouput something like:

    [ ] test.db        test
    [ ] test2.db       test2

But now I need to be able to access those widgets to build out a command. That list could be any number of .db files based on what the scrubDB function returns.

I'm still pretty new to Python and wxPython, so I would appreciate any guidance here.


Solution

  • This ended up working:

    for db in numDB:
                check = wx.CheckBox(self, -1, db)
                sizer.Add(check, pos=(xIndex,0), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=10)
                label = wx.StaticText(panel, label="")
                sizer.Add(label, pos=(xIndex,1), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=10)
                name = wx.TextCtrl(panel)
                #Set Temp Name
                if db.endswith('.db'):
                    name.Value = db[:-3]
                sizer.Add(name, pos=(xIndex,2), span=(1,3),flag=wx.EXPAND|wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.TOP, border=5)
                xIndex +=1
    
        #-------------------------------------------------------
        # Save references to the widgets created dynamically
                list_checkboxID.append(check.GetId())
                list_checkboxLabel.append(check.GetLabel())
                list_txtctrlID.append(name.GetId())
                list_txtctrlLabel.append(name.Value)
    
                #Bind whatever events you want here -
                check.Bind(wx.EVT_CHECKBOX, self.OnCheck, check)
    
        def OnCheck(self, event):
            for item in range(len(list_checkboxID)):
                print "Checkbox " + str(item) + ":\t\t\tID:" + str(list_checkboxID[item]) + "\tLABEL:" + list_checkboxLabel[item]
                print "Text Control " + str(item) + ":\t\tID:" + str(list_txtctrlID[item]) + "\tLABEL:" + list_txtctrlLabel[item]
    

    I completely realize there is probably a smarter way to manage the tuple (which I learned all about in the process of trying to figure this out. :)