本文介绍了在运行时在 wxPython 中添加复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

# -*- 编码:utf-8 -*-########################################################################## 使用 wxFormBuilder 生成的 Python 代码(2011 年 6 月 30 日版本)## http://www.wxformbuilder.org/#### 请不要不要"编辑这个文件!########################################################################进口 wx导入操作系统########################################################################## 课堂测试########################################################################类测试( wx.Frame ):扩展=[]def __init__(自我,父母):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"选择根文件夹", 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.root_dir_pick.Bind( wx.EVT_DIRPICKER_CHANGED, self._ScanRoot )def __del__(自我):经过# 虚拟事件处理程序,在您的派生类中覆盖它们def _ScanRoot( self, event ):分机=''对于 os.walk(self.root_dir_pick.GetPath()) 中的 root、dirs、文件:对于文件中的 f:尝试:ext = os.path.splitext(f)self.extensions.index(ext[1])除了:self.extensions.append(ext[1])对于 self.extensions 中的 ext1:chb=wx.CheckBox( bSizer_Ext, wx.ID_ANY, ext1, wx.DefaultPosition, wx.DefaultSize, 0 )如果 __name__ == "__main__":app = wx.App()框架 = 测试(无)app.MainLoop()
解决方案

修改_ScanRoot中最后一个for循环如图:

 用于 self.extensions 中的 ext1:chb = wx.CheckBox(self, wx.ID_ANY, ext1, wx.DefaultPosition, wx.DefaultSize, 0 )self.bSizer_Ext.Add(chb)self.Layout()

同时在程序中用self.bSizer_Ext

修改bSizer_Ext的所有实例

此代码尚未起作用.您需要:

  • 将复选框绑定到事件以检测点击
  • 保持每个复选框对象可用于在需要时对其进行修改(这实际上是可选的)

例如,这是一个替代方案:

 def OnCheck(self, evt):"""在这里我将决定采取哪些行动"""obj = evt.GetEventObject()如果 obj.IsChecked():打印%s 选定"% obj.Label别的:打印%s 取消选择"% obj.Label# 虚拟事件处理程序,在您的派生类中覆盖它们def _ScanRoot( self, event ):分机 = ''对于 os.walk(self.root_dir_pick.GetPath()) 中的根、目录、文件:对于文件中的 f:尝试:ext = os.path.splitext(f)self.extensions.index(ext[1])除了:self.extensions.append(ext[1])对于 self.extensions 中的 ext1: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()

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

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

这篇关于在运行时在 wxPython 中添加复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:22