2010-09-10 10 views
1

這裏是PythonCard一個樣本資源文件:如何動態獲取GUI類中所有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是不可迭代的。

+0

我想答案與此有關:HTTP ://stackoverflow.com/questions/2352181/how-to-use-a-dot-to-access-members-of-dictionary – 2011-03-20 21:23:42

+0

你解決了你的問題嗎? – 2011-06-30 02:14:31

回答

0

這將是,如果你能得到從其他地方的組件列表中乾淨多了,但我認爲,如果你這樣做它應該工作:

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