在运行时在wxPython中添加复选框

时间:2012-01-07 03:18:23

标签: python wxpython runtime

我对wxPython很新,所以我使用wxFormBuilder生成GUI并尝试从某个文件夹获取扩展并使用这些exts创建复选框,但我得到了

  
    

NameError:未定义全局名称'bSizer_Ext'

  

任何建议将不胜感激。谢谢。

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

1 个答案:

答案 0 :(得分:1)

修改_ScanRoot中的最后一个for循环,如下所示:

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

同时在程序中修改bSizer_Ext self.bSizer_Ext

的所有实例

此代码尚无功能。你需要:

  • 将您的复选框绑定到活动以检测点击次数
  • 保留每个复选框对象,以便在需要时修改它们(这实际上是可选的)

例如,这是另一种选择:

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