@ramvee , not sure you are aware of the below or not. The PYUILoader was built up by @Jonb after some questions on the forums here. Basically it allows a loaded UIFile to look and act as a custom class. Not just a wrapper. If you do a search in the forum you will come up with quite a bit about it. As far as I know, the below is the most refined version. As it handles custom properties set in the pyui file also. Honestly, its worth a look. I have gone backwards and forwards on using UIFiles. I think were i will end up falling is that for very quick and dirty things as well as examples etc, i will not bother. But once I start to make something someone might possibly use other than me, then I think I should UIFiles. Several of the guys here that really know what they are doing have expressed their views on the complexity of maintaining ui.Views that start to get a little complex, using flex etc. Anyway, each to their own.
Hope the below is helpful to someone.
import ui
class PYUILoader(ui.View):
# this acts as a normal Custom ui.View class
# the root view of the class is the pyui file read in
def WrapInstance(obj):
class Wrapper(obj.__class__):
def __new__(cls):
return obj
return Wrapper
def __init__(self, pyui_fn, *args, **kwargs):
bindings = globals().copy()
bindings[self.__class__.__name__] = self.WrapInstance()
ui.load_view(pyui_fn, bindings)
# call after so our kwargs modify attrs
super().__init__(*args, **kwargs)
class MyClass(PYUILoader):
'''
Just have to be mindful of a few things.
1. Set your instance vars before the super is called, otherwise
they will not be accessible in the did_load.
2. Do any form element setup in the did_load call back. The ui module
call this method.
3. In the in the UIFile in the 'Custom View Class' section you must
put in the name of this class. In this case MyClass
4. I like the dict update in the did_load method. Nothing to do with
getting this class setup. But its just a quick short cut for setting
instance vars to your form elms for you can access them with dot notation.
of course you could have just done self.label1 = self['lablel1'].
Just a nice short cut.
'''
def __init__(self, uifilename, *args, **kwargs):
self.set_instance_vars_here = 'Yes you got me'
super().__init__(uifilename, *args, **kwargs)
def did_load(self):
print(self.set_instance_vars_here)
# add all instance vars for the form elements to the class.
# its ok, but the late binding means the editor can not help
# still better than string indexs I think.
# This is not recursive. It could be though.
[self.__dict__.update({sv.name: sv}) for sv in self.subviews]
self['label1'].text = 'My Text'
self.label1.text = 'My Text'
if __name__ == '__main__':
uifilename = 'test1.pyui'
f = (0, 0, 300, 400)
v = MyClass(uifilename, frame=f)
v.present(style='sheet', animated=False)