I have a Kivy app that has text fields who's values I want to back in a dictionary (and persist by storing on disk as a JSON save file). I am having some trouble with this architecture, namely updating a value in the nested dictionary that backs the options in the UI.
data
{
'general':
{
'notes': '<notes>',
'shift': 'NOC',
'runs': 4
},
'notes':
{
'feeders': [<values>],
'fluids': [<values>]
}
}
Using Kivy, I have UI text boxes associated with each leaf key in the nested dictionary, and need to update the leaf key when the corresponding text box value is changed.
For example, when my text box for "General Notes" is updated, I need to update the value stored in data['general']['notes'].
In Kivy, I have for myapp.py
:
class TextEntry(TextInput):
default_text = StringProperty("Enter a value..")
def on_text(self, instance, value):
<data> = value if value is not self.default_text else <data>
def text_init(self):
return <data> if not (<data> is self.default_text or <data> == "") else self.default_text
Anywhere you see <data>
, I need to know a specific key for a specific value in data
, but <data>
will vary for each instance of TextEntry
.
and in my.kv
I have:
<Some Layout>
Label:
text: 'Note'
TextEntry:
default_text: "Enter notes..."
text: self.text_init()
The thing I'm struggling with, is that the class TextEntry
is independent of the <data>
key/value pair, so for any instance of TextEntry
I do not know the specific key/value pair for <data>
for that instance.
The trouble is this:
- I can't simply store a reference to the value of
<data>
in a variabledata_val
on the instance and pass the reference around because theid(<data>)
is different from anydata_val= <data>; id(data_val)
. - Unlike a list slice object, a dictionary key can't be stored as an object (that always returns the value for that key when passed to
mydict[<keyobject>]
), so that I can't simply store a key object in a variabledata_key
on the instance and pass that around either.
Am I going about this wrong using a dictionary to back values in the UI? Or is there a way to do this with a dictionary that I'm not seeing? Or possibly, is this a silly architecture and there is a better way to handle these kinds of transactions that I'm not aware of?
Thank you for help and pointers!