Atom api是Enaml用来实现MVC的库。更改原子var,并更新UI。在UI中更改它,并更新模型。Python Atom API:使用字典时如何设置atom var
我想提出(在这种情况下,布尔())一个Atom变种成一本字典,后来更新变种
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
我简直要疯了,但是这看起来像你试图重新分配地方变量(副本)。您可能需要使用'set'操作修改字典。 – Carcigenicate
您的代码至少有一个语法错误,请确保您发布的内容是[MCVE]。你对Python OOP有多熟悉?多件事情有点关闭......而你的具体问题是名字'v'正在被反弹;相反,您需要存储该词典并将其改为循环。除了所有其他问题外, 'tmpdict = getDict();对于tmpdict中的k:tmpdict [k] = True'。 –
你是对的@Carcigenicate,一个简单的变量赋值正在发生。本地var和myBool的id()是相同的。有没有可以调用来更新原始变量的函数?我会看看集合。 –