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.
Creating a PYUI file that expands automatically on iPad?
-
Hi all! Long time lurker here.
I made my first pythonista app with a PYUI file. The learning curve has been really fun and interesting - started off with some basic python tutorials and then just jumped right in!
It took a good while to figure out how to send my newly created 'application' through to my iPad, but I exported it as jSON and imported it back in.
However, I am struggling to find a way to create a pyui file that works on both an iphone and resizes automatically for a full on iPad. Is there a way to do that, or do I need to develop on iPad and iOS independently?
Many thanks!
-
I do not know much about the
ui
module but as far as I know the auto-resize feature of it has its limits.- each
ui.View
has aflex
field which allows some sort auf auto-resizing in relation to its parent. But it doesn't allow complex dependencies like for exampleXAML
does. - each
ui.View
also has alayout()
method which is called after its parent has been resized. Here you could intervene and adjust the layout of yourui
node when its parent (the main form) has been resized. - edit: technically you could also implement a general solution (an
ui.view
looking at its children and positioning and resizing them according to an algorithm) but that would require to implement stuff likemin_size
for everything. - if I remember correctly there are also some constants floating around in the
ui
module (check at the bottom of the doc) that tell you about the orientation and type of device your script is running on.
- each
-
@Aaraeus , the below is some code that expands on the pyui abilities. At least I think it does. Does not really answer your question, but you can do a lot with pyui files. The PYUILoader code was written by @JonB. It's difficult to show all you can do with this code, but if you search the forum you will see a lot of discussion. Eg. Subviews in the pyui file can have its 'Custom View Class' property set to a python class. Then you can resize that element, set flex attrs etc as you wish at runtime. Just some food for thought.
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 # code from @JonB 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 MyView(PYUILoader): def __init__(self, pyui_fn, *args, **kwargs): super().__init__(pyui_fn, *args, **kwargs) if __name__ == '__main__': w, h = 600, 800 f = (0, 0, w, h) pyui_file_name ='my_pyui.pyui' # has to be a vaild filename to a UI.File ''' for this code to work, you need to set the 'Custom View Class' property in the pyui file to the name of the class. in this case its 'MyView'. ''' mc = MyView(pyui_file_name, frame = f, bg_color = 'deeppink') mc.present('sheet')