pyto_ui

UI for scripts

The pyto_ui module contains classes for building and presenting a native UI, in app or in the Today Widget. This library’s API is very similar to UIKit.

Warning

This library requires iOS / iPadOS 13.

This library may have a lot of similarities with UIKit, but subclassing isn’t supported very well. Instead of overriding methods, you will often need to set properties to a function. For properties, setters are what makes the passed value take effect, so instead of override the getter, you should just set properties. If you really want to subclass a View, you can set properties from the initializer.

(Many docstrings are quoted from the Apple Documentation)

Getting Started

Each item presented on the screen is a View object. This module contains many View subclasses.

We can initialize a view like that:

import pyto_ui as ui

view = ui.View()

You can modify the view’s attributes, like background_color for example:

view.background_color = ui.COLOR_SYSTEM_BACKGROUND

Then, call the show_view() function to show the view:

ui.show_view(view, ui.PRESENTATION_MODE_SHEET)

A view will be presented, with the system background color, white or black depending on if the device has dark mode enabled or not. It’s important to set our view’s background color because it will be transparent if it’s not set. That looks great on widgets, but not in app.

NOTE: The thread will be blocked until the view is closed, but you can run code on another thread and modify the UI from there:

ui.show_view(view)
print("Closed") # This line will be called after the view is closed.

Now we have an empty view, the root view, we can add other views inside it, like a Button:

button = ui.Button(title="Hello World!")
button.size = (100, 50)
button.center = (view.width/2, view.height/2)
button.flex = [
    ui.FLEXIBLE_TOP_MARGIN,
    ui.FLEXIBLE_BOTTOM_MARGIN,
    ui.FLEXIBLE_LEFT_MARGIN,
    ui.FLEXIBLE_RIGHT_MARGIN
]
view.add_subview(button)

We are creating a button with title “Hello World!”, with 100 as width and 50 as height. We place it at center, and we set flex to have flexible margins so the button will always stay at center even if the root view will change its size.

To add an action to the button:

def button_pressed(sender):
    sender.superview.close()

button.action = button_pressed

We define a function that takes the button as parameter and we pass it to the button’s action property. The superview property of the button is the view that contains it. With the close() function, we close it.

So we have this code:

import pyto_ui as ui

def button_pressed(sender):
    sender.superview.close()

view = ui.View()
view.background_color = ui.COLOR_SYSTEM_BACKGROUND

button = ui.Button(title="Hello World!")
button.size = (100, 50)
button.center = (view.width/2, view.height/2)
button.flex = [
    ui.FLEXIBLE_TOP_MARGIN,
    ui.FLEXIBLE_BOTTOM_MARGIN,
    ui.FLEXIBLE_LEFT_MARGIN,
    ui.FLEXIBLE_RIGHT_MARGIN
]
button.action = button_pressed
view.add_subview(button)

ui.show_view(view, ui.PRESENTATION_MODE_SHEET)

print("Hello World!")

When the button is clicked, the UI will be closed and “Hello World!” will be printed. UIs can be presented on the Today widget if you set the widget script.