-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathuse-a-pyui-file-in-another-pyui-file.py
153 lines (119 loc) · 3.3 KB
/
use-a-pyui-file-in-another-pyui-file.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# coding: utf-8
# https://forum.omz-software.com/topic/3176/use-a-pyui-file-in-another-pyui-file/10
import ui
import os
class PYUILoaderStr(ui.View):
'''
loads a pyui file into the class, acts as another ui.View
class.
** Please note that the pyui class must have its
Custom Class attr set to selfwrapper
Thanks @JonB
'''
def __init__(self, pyui_str, raw = True):
# black magic here, for me at least...
class selfwrapper(ui.View):
def __new__(cls):
return self
if raw:
pyui_str = json.dumps(pyui_str)
ui.load_view_str(pyui_str,
bindings={'selfwrapper':selfwrapper, 'self':self})
def xx():
print 'hi'
return True
class PYUILoader(ui.View):
'''
loads a pyui file into the class, acts as another ui.View
class.
** Please note that the pyui class must have its
Custom Class attr set to selfwrapper
Thanks @JonB
'''
def __init__(self, f_name = None):
print 'in PYUILoader init'
# black magic here, for me at least...
class selfwrapper(ui.View):
def __new__(cls):
return self
if not f_name.endswith('.pyui'):
f_name += '.pyui'
# make sure the file exists
if not os.path.isfile(f_name):
raise OSError
ui.load_view( f_name ,
bindings={'selfwrapper':selfwrapper, 'self':self})
class MyClass(PYUILoader):
def __init__(self, f_name ):
PYUILoader.__init__(self, f_name)
self.width = 500
self.height = 500
print 'in Myclass'
self['menu'].bg_color = 'red'
def xx(self):
print 'hello from my class'
return True
if __name__ == '__main__':
mc = MyClass('StdView')
mc.present('sheet', animated = False)
# --------------------
v = ViewClass()
v.frame = _str2rect(view_dict.get('frame'))
v.flex = attrs.get('flex', '')
v.alpha = attrs.get('alpha', 1.0)
v.name = attrs.get('name')
...
return v
...
# --------------------
# wrapper.py
def WrapInstance(obj):
class Wrapper(obj.__class__):
def __new__(cls):
return obj
return Wrapper
#MyView.py
from wrapper import WrapInstance
class MyView(ui.View):
def __init__(self):
ui.load_view('MyView',bindings={'MyView':WrapInstance(self),'self':self})
# --------------------
import ui
# wrapper.py, Pythonista Forum @JonB
# https://forum.omz-software.com/topic/3176/use-a-pyui-file-in-another-pyui-file
# remember to add the the name of the class to the 'Custom View Class'
# in the .pyui
_pyui_file_name = 'find.pyui'
def WrapInstance(obj):
class Wrapper(obj.__class__):
def __new__(cls):
return obj
return Wrapper
class MyClass(ui.View):
def __init__(self, *args, **kwargs):
ui.load_view(_pyui_file_name,
bindings={'MyClass': WrapInstance(self), 'self': self})
super().__init__(*args, **kwargs)
if __name__ == '__main__':
w = 600
h = 325
f = (0, 0, w, h)
mc = MyClass(bg_color='deeppink')
mc.present('sheet')
# --------------------
ui.load_view(_pyui_file_name,
bindings={self.__class__.__name__: WrapInstance(self), 'self': self})
# --------------------
def pyui_bindings(obj):
def WrapInstance(obj):
class Wrapper(obj.__class__):
def __new__(cls):
return obj
return Wrapper
bindings = globals().copy()
bindings[obj.__class__.__name__]=WrapInstance(obj)
return bindings
class PYUIClass(ui.View):
def __init__(self, pyui_fn, *args, **kwargs):
ui.load_view(pyui_fn, pyui_bindings(self))
super().__init__(*args, **kwargs)