@wolf71 , the below is a little more than you are looking for. But it's great, not my code btw. I just did a wrapper. But the below code loads a pyui/UIFile into a Custom ui class. Opposed to loading a view into a Custom Class. Anyway, if you like using pyui files, this is so nice. But more you can do also. Just search the forum for pyui.
You can do a lot in pyui files. Define a custom clsss , mapping it to code also custom attributes. It's a long conversation, but you can do a lot.
#Pythonista Forum - @Phuket2
import ui, editor
def pyui_bindings(obj):
# Pythonista Forum, @JonB
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
# a Custom ui class as it subclasses ui.View
class MYUIClass(ui.View):
def __init__(self, ui_file, *args, **kwargs):
ui.load_view(ui_file, pyui_bindings(self))
super().__init__(*args, **kwargs)
# this is a callback from ui, is called automatically for a custom
# class, if the method is defined
def will_close(self):
print('the view will close')
def did_load(self):
print('the pyui file has loaded...')
if __name__ == '__main__':
_use_theme = True
w, h = 320, 320
f = (0, 0, w, h)
ui_file = 'myui'
style = 'sheet'
animated = False
theme = 'Oceanic'
hide_title_bar = False
mc = MYUIClass(ui_file, frame=f, bg_color='white')
if not _use_theme:
mc.present('sheet', animated=animated,
hide_title_bar=hide_title_bar)
else:
mc.name = theme
editor.present_themed(mc, theme_name=theme,
style=style, animated=animated,
hide_title_bar=hide_title_bar)
Edit, sorry, for this to work, you need to set the Custom Class attr in the pyui file to the class name, in this case MYUIClass