如何动态获取GUI类中所有PythonCard组件的列表?

2024-05-14 11:10:44 发布

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

以下是PythonCard的示例资源文件:

{ 'application':{ 'type':'Application',
            'name':'Test Sounds',

    'backgrounds':
 [ 
  { 'type':'Background',
    'name':'Test Sounds',
    'title':'Test Sounds',
    'position':( 5, 5 ),
    'size':( 300, 200 ),

   'components':
   [ 
    { 'type':'TextField', 'name':'fldFilename', 'position':( 5, 4 ), 'size':( 200, -1 ), 'text':'anykey.wav' },
    { 'type':'Button', 'name':'btnFile', 'position':( 210, 5 ), 'size':( -1, -1), 'label':'Select File' },
    { 'type':'Button', 'name':'btnPlay', 'position':( 5, 40 ), 'size':( -1, -1 ), 'label':'Play' },
    { 'type':'CheckBox', 'name':'chkAsync', 'position':( 5, 70 ), 'size':( -1, -1 ), 'label':'Async I/O', 'checked':0 },
    { 'type':'CheckBox', 'name':'chkLoop', 'position':( 5, 90 ), 'size':( -1, -1 ), 'label':'Loop sound', 'checked':0 },
   ] } ] } }

使用此源文件:

^{pr2}$

我该如何从GUI类中动态地获取所有“Button”组件的列表(例如)?在

组件是以self.components.<component name>的方式访问的,所以我最初的想法是for x in self.components: ...,但是{}是不可编辑的。在


Tags: nametestself示例sizetypecomponents组件
1条回答
网友
1楼 · 发布于 2024-05-14 11:10:44

如果您能够从其他地方获得组件列表,则会更干净,但我认为,如果您执行以下操作,它应该会有效:

for comp_name in dir(self.components):
    if comp_name.startswith('_'):    # ignore special members like __repr__
        continue
    component = getattr(self.components, comp_name)
    ...

相关问题 更多 >

    热门问题