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

发布于 2024-11-25 17:00:41 字数 1198 浏览 2 评论 0原文

因此,我在这里有一个小项目,它搜索文件 (*.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)

这会输出类似以下内容:

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

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

我对 Python 和 wxPython 还很陌生,所以我很感激这里的任何指导。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热血少△年 2024-12-02 17:00:41

您将需要动态绑定事件 -

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

You will need to bind the events dynamically -

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
子栖 2024-12-02 17:00:41

这最终成功了:

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]

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

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. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文