1

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 },
   ] } ] } }

このソース ファイルを使用すると、次のようになります。

from PythonCard import model

class Sounds(model.Background):

    # Some irrelevant methods... #

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

GUIクラス内からすべての「ボタン」コンポーネント(たとえば)のリストを動的に取得するにはどうすればよいですか?

コンポーネントはその方​​法でアクセスされるself.components.<component name>ので、私の最初の考えは でしfor x in self.components: ...たが、self.components反復可能ではありません。

4

1 に答える 1

0

他の場所からコンポーネントのリストを取得できれば、はるかにクリーンになりますが、次のようなことを行うと機能するはずです。

for comp_name in dir(self.components):
    if comp_name.startswith('_'):    # ignore special members like __repr__
        continue
    component = getattr(self.components, comp_name)
    ...
于 2011-05-25T06:34:23.590 に答える