开发者

How do I dynamically get a list of all PythonCard components in a GUI class?

Here is a sample resource file for 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 },
   ] } ] } }

With this source file:

from PythonCard import model

class Sounds(model.Background):

    # Some irrelevant methods... #

if __name__ == '__main__':
    app = model.Application(Sounds)
    app.MainLoop()

How would I go about dynamically obtaining a list of all the "Button" components (for example) from within the GUI class?

Components are accessed in the manner self.components.<component name> so my initial thought was for x in self.components: ..., but self.components is not iterable.


It would be much cleaner if you were able to get the list of components from elsewhere, but I think it should work if you do something like:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜