The Atom api https://pypi.python.org/pypi/atom/0.3.6是Enaml用来实现MVC的库。更改一个原子变量,UI 就会更新。在 UI 中更改它,您的模型就会更新。
我想将 Atom var(在本例中为 Bool())放入字典中,然后更新该 var
from atom.api import Atom,Bool
class MyModel(Atom):
myBool = Bool()
def getDict(self):
return {'mybool':self.myBool}
def setAllBoolsTrue(self):
self.myBool = True #example to show that just setting mybool will update UI components that use it
#now to show how I'd like to generalize to many components
for k,v in self.getDict().iteritems():
v = True # this fails, even though the id(v) is the same as id(self.mybool)
最后一条语句未能更新 mybool,它只是进行了简单的赋值。
那么有没有一种方法可以像简单设置一样更新从字典中检索的 Bool() 呢?
编辑:代码已更新,因此没有语法错误。
编辑:根据评论中的建议,我尝试过但没有成功:
tempDict = self.getDict();
#self.myBool = True # this works
tempDict['mybool'] = True #this does not work
for k, v in getDict():
不会工作,除非你的getDict()
函数返回两个键的字典,即k, v
会更恰当地命名key1, key2
在这种情况下,其中key2
甚至不存在。
如果你真的想实现类,你可以做类似的事情......
class MyModel(Atom):
def __init__(self):
self.myBool = True
>>> model = MyModel()
>>> model.myBool
True
>>> model.myBool = False
>>> model.myBool
False
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)