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()是相同的。有沒有可以調用來更新原始變量的函數?我會看看集合。 –