-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_value_access.py
44 lines (34 loc) · 1.33 KB
/
model_value_access.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
This example shows how to deal with "deep" models. (dict or list valued models)
"""
from quasargui import *
dic_model = Model({})
dic_model['color'] = '#aaa'
print(dic_model['color'].value) # ~> #aaa
dic_model['data'] = []
dic_model['data'].value.append('Item 1')
print(dic_model['data'][0].value) # ~> Item 0
try:
print(dic_model['data'][1].value) # raises IndexError
except IndexError:
print("dic_model['data'][1] call raised index error")
print(dic_model.value) # ~> {'color': '#aaa', 'data': ['Item 1']}
normal_model = Model('normal')
deep_model = Model({'deep': {'data': 'deep value'}})
print(deep_model['deep']['data'].value) # ~> deep value (but this creates 2 extra models)
print(deep_model.value['deep']['data']) # ~> deep value (this just retrieves the value)
deep_data = deep_model['deep']['data']
list_model = Model(['apple', 'orange'])
list0 = list_model[0]
layout = QLayout([QPage([
QInput('normal:', normal_model, events={
'keyup': lambda: layout.notify("Value is {}".format(normal_model.value))
}),
QInput('deep.data:', deep_data, events={
'keyup': lambda: layout.notify("Value is {}".format(deep_data.value))
}),
QInput('list[0]:', list0, events={
'keyup': lambda: layout.notify("Value is {}".format(list0.value))
}),
])])
run(layout, _render_debug=True, debug=True)