简体   繁体   中英

Add checkbox in wxPython in runtime

I am pretty new to wxPython, so I generate GUI with wxFormBuilder and try to get extension from some folder and to create checkbox with these exts, but I got

NameError: global name 'bSizer_Ext' is not defined

Any suggestion will be appreciated. Thank you.

# -*- coding: utf-8 -*- 

###########################################################################
## Python code generated with wxFormBuilder (version Jun 30 2011)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import os

###########################################################################
## Class Test
###########################################################################

class Test ( wx.Frame ):
    extensions=[]
    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Test", pos = wx.DefaultPosition, size = wx.Size( 600,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        fg_DirSizer = wx.FlexGridSizer( 0, 2, 0, 2 )
        fg_DirSizer.AddGrowableCol( 1 )
        fg_DirSizer.SetFlexibleDirection( wx.BOTH )
        fg_DirSizer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )

        self.m_staticText_root = wx.StaticText( self, wx.ID_ANY, u"Root Directory", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText_root.Wrap( -1 )
        fg_DirSizer.Add( self.m_staticText_root, 0, wx.ALL, 5 )

        self.root_dir_pick = wx.DirPickerCtrl( self, wx.ID_ANY, wx.EmptyString, u"Select root folder", wx.DefaultPosition, wx.DefaultSize, wx.DIRP_DEFAULT_STYLE|wx.DIRP_DIR_MUST_EXIST|wx.TAB_TRAVERSAL )
        self.root_dir_pick.SetMinSize( wx.Size( 450,25 ) )

        fg_DirSizer.Add( self.root_dir_pick, 0, wx.ALL, 5 )

        bSizer1.Add( fg_DirSizer, 0, wx.EXPAND, 5 )

        bSizer_Ext = wx.BoxSizer( wx.VERTICAL )

        bSizer1.Add( bSizer_Ext, 1, wx.EXPAND, 5 )

        self.SetSizer( bSizer1 )
        self.Layout()

        self.Centre( wx.BOTH )
        self.Show(True)

        # Connect Events
        self.root_dir_pick.Bind( wx.EVT_DIRPICKER_CHANGED, self._ScanRoot )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def _ScanRoot( self, event ):
        ext=''
        for root,dirs,files in os.walk(self.root_dir_pick.GetPath()):
            for f in files:
                try:
                    ext = os.path.splitext(f)
                    self.extensions.index(ext[1])
                except:
                    self.extensions.append(ext[1])
        for ext1 in self.extensions:
            chb=wx.CheckBox( bSizer_Ext, wx.ID_ANY, ext1, wx.DefaultPosition, wx.DefaultSize, 0 )


if __name__ == "__main__":
    app = wx.App()
    frame = Test(None)
    app.MainLoop()

Modify the last for loop in _ScanRoot as shown:

for ext1 in self.extensions:
    chb = wx.CheckBox(self, wx.ID_ANY, ext1, wx.DefaultPosition, wx.DefaultSize, 0 )
    self.bSizer_Ext.Add(chb)
self.Layout()

Also modify in the program all instances of bSizer_Ext with self.bSizer_Ext

This code is no functional yet. You need to:

  • Bind your checkboxes to an event in order to detect clicks
  • Keep each checkbox object available to modify them if needed (this is actually optional)

For example, this is an alternative:

    def OnCheck(self, evt):
        """Here I will decide which actions to take"""
        obj = evt.GetEventObject()
        if obj.IsChecked():
            print "%s selected" % obj.Label
        else:
            print "%s deselected" % obj.Label

    # Virtual event handlers, overide them in your derived class
    def _ScanRoot( self, event ):
        ext = ''
        for root, dirs, files in os.walk(self.root_dir_pick.GetPath()):
            for f in files:
                try:
                    ext = os.path.splitext(f)
                    self.extensions.index(ext[1])
                except:
                    self.extensions.append(ext[1])
        for ext1 in self.extensions:
            chb = wx.CheckBox(self, wx.ID_ANY, ext1, wx.DefaultPosition, wx.DefaultSize, 0 )
            self.bSizer_Ext.Add(chb)
            self.Bind(wx.EVT_CHECKBOX, self.OnCheck, chb)
        self.Layout()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM