Components¶
High-level overview of PythonNative components and how they map to native UI.
Constructor pattern¶
- All core components share a consistent, contextless constructor on both platforms.
- On Android, a
Contextis acquired implicitly from the currentActivityset bypn.Page. - On iOS, UIKit classes are allocated/initialized directly.
Examples:
import pythonnative as pn
class MainPage(pn.Page):
def __init__(self, native_instance):
super().__init__(native_instance)
def on_create(self):
super().on_create()
stack = pn.StackView()
stack.add_view(pn.Label("Hello"))
stack.add_view(pn.Button("Tap me"))
stack.add_view(pn.TextField("initial"))
self.set_root_view(stack)
Notes:
- pn.Page stores the Android Activity so components like pn.Button() and pn.Label() can construct their native counterparts.
- If you construct views before the Page is created on Android, a runtime error will be raised because no Context is available.
Core components¶
Stabilized with contextless constructors on both platforms:
PageStackViewLabel,ButtonImageViewTextField,TextViewSwitchProgressView,ActivityIndicatorViewWebView
APIs are intentionally small and grow progressively in later releases. Properties and setters are kept consistent where supported by both platforms.
Platform detection and Android context¶
- Use
pythonnative.utils.IS_ANDROIDfor platform checks when needed. - On Android,
Pagerecords the currentActivityso child views can acquire aContextimplicitly. Constructing views beforePageinitialization will raise.