I see some pretty old threads about this topic, but I was wondering what the current definitive "best practices" way is to create a custom View subclass which can load itself from a .pyui file, and hook up the actions of its subviews to instance methods?
So something along the lines of:
class MyView(ui.View):
def __init__(self):
ui.load_view(...)
def buttonAction(self, sender):
print "button pressed:",sender
instance = MyView()
instance.present()
Where buttonAction is set in the UI editor as the Action attribute of a Button that is a subview of the base view in the pyui file.
Now, obviously the above doesn't work, since if I try to load a .pyui which has a base view set to the custom view subclass "MyView" calling "ui.load_view()" is recursive, trying to insantiate a MyView which in turn loads the pyui which needs to instantiate another MyView, etc. And if I try to have the base view in the pyui file just be a plain ui.View class, calling ui.load_view() in the init() of my custom class doesn't appear to load anything, at just creates an empty view.
There's an old thread from two years ago discussing a way of doing this by wrapping the instantiation:
https://forum.omz-software.com/topic/2154/using-a-custom-pu-pyui-view-in-another-one-using-ui-editor/12
specifically this bit:
class MyView(ui.View):
def __init__(self, *args, **kwargs):
class selfwrapper(ui.View):
def __new__(cls):
return self
if kwargs.get('pyui_file'):
ui.load_view(... ,bindings={'selfwrapper':selfwrapper, 'self':self})
Is something like that still the right way to do this?