Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Relating to loading a UIfile into a Custom Class
-
@JonB , sorry but this question is mainly directed to you. If anyone else can answer, thank you.
But the code below loads a UIFile/pyui file into a Custom ui.View Class.
If I create a Custom View in the UIFile and set the Custom View Class to UserItem it all works.
But in code, the superview is None.
My question is, is there a way for UserItem, in this case to know it's superview is MyClass?
I have tried some different things. But I just can't get it.
Sorry, I know this is a little vague, but some people will know what I am talking about.import ui class UserItem(ui.View): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.make_view() print(self.superview) def make_view(self): pass class PYUILoader(ui.View): 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): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.make_view() def make_view(self): pass if __name__ == '__main__': w, h = 600, 800 f = (0, 0, w, h) # any UIFile, with a customview added, then for the customview # Custom View Class attr = UserItem ui_file = 'hcard' style = 'sheet' mc = MyClass(ui_file, frame = f, bg_color = 'white') mc.present(style=style)
-
@Phuket2 In
__init__
, the view can't know its superview - because of the design of UIKit and theui
module, the superview has to be set indirectly usingadd_subview
on another view, which is only possible after the subview is created.__init__
runs during creation of the view, so the superview can't be set there yet.You can however define a
did_load
method in your custom subclass. Theui
module's pyui loading mechanism (ui._view_from_dict
) callsdid_load
on each view when it is fully loaded, i. e. after all attributes are set and after it is added to its superview. So you can do anything there that requires access to the superview or other attributes from the pyui file. -
@dgelessus , thanks makes sense what you say, and I should have tried did_load. And it works as long as I do it in MyClass and not UserItem which makes sense we'll sort of. Depending on the load order I guess. It's just a shame. Would have been nice to access the superview in the UserItem at some point in the load process.
As an FYI, the name attr does not seem to work in the UserItem. Meaning if I set it, has no effect on its subview name. I am not sure it should in the case, but it seems like it should.
But again thanks. At least I know I am am just not making extra work for myself