-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathVars.py
executable file
·72 lines (49 loc) · 1.97 KB
/
Vars.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import copy
# Vars is a base class to override pickling
class Vars(object):
dict_vars = {}
# PICKLING
# All class objects are pickled normally except for
# TkinterVars. The object methods are skipped.
def __getstate__(self):
dict_data = self.__class__.__dict__.copy()
for k in list(dict_data):
v = dict_data[k]
# object is not a method
if not k.startswith('__'):
# object is a StringVar, IntVar, etc.
name = v.__class__.__name__
if name == 'StringVar' or name == 'IntVar' or \
name == 'BooleanVar' or name == 'DoubleVar':
del dict_data[k]
try:
dict_data['_' + k] = v.get()
except:
pass
elif k == 'dict_vars':
del dict_data[k]
else:
del dict_data[k]
# Copy instance variables
dict_data.update(self.__dict__)
return dict_data
# UNPICKLING
# First unpickle objects, list and dict
# Then Vars because they are bound to controls
def __setstate__(self, dict_data):
Vars.dict_vars.clear()
for k in list(dict_data):
v = dict_data[k]
# object is a StringVar, IntVar, etc.
if k.startswith('_'):
svar = k[1:]
Vars.dict_vars[svar] = v
del dict_data[k]
self.__dict__.update(dict_data)
# refresh the values of Vars to trigger Tracers
def refresh(self):
for k, v in Vars.dict_vars.items():
var = self.__class__.__dict__.get(k)
if var is not None:
var.set(v)
self.dict_vars.clear()