动态填充小部件,然后如何访问它们?

2024-05-29 03:42:02 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我在这里有一个小项目,它搜索文件(*.db)的路径,然后为这些小部件创建一个复选框和文本控件。当我运行应用程序时,这部分工作得很好:

    # 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)

这会产生如下结果:

^{pr2}$

但是现在我需要能够访问这些小部件来构建命令。根据scrubDB函数返回的内容,该列表可以是任意数量的.db文件。在

我对Python和wxPython还是相当陌生的,所以我很感谢这里的任何指导。在


Tags: nameposaddfordblabelflagcenter
2条回答

您需要动态绑定事件-

list_checkbox = []
list_textctrl = []

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_checkbox += check
    list_textctrl += name

    # Bind whatever events you want here - 
    frame.Bind(wx.EVT_CHECKBOX, OnCheck, check)

def OnCheck(*args):
    if args[0].IsChecked():              # args[0] holds the reference to the widget which has triggered this event.
        id = list_checkbox.index(args[0])
        adj_textctrl = list_textctrl[id]          # this is your textctrl next to your checkbox
        # do whatever you want to do

这最终奏效了:

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]

我完全意识到可能有一种更聪明的方法来管理元组(这是我在尝试解决这个问题的过程中学到的)。:)

相关问题 更多 >

    热门问题