Atom api是 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
关于Python 原子 API : how to set atom var when using a dictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41253798/