Skip to content

Commit 9a3d431

Browse files
committed
Add LinearLayout to pythonnative.py
1 parent 44f8d18 commit 9a3d431

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

apps/pythonnative_demo/app/main.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pythonnative as pn
2+
3+
4+
def main():
5+
# Create a main view
6+
main_view = pn.View()
7+
8+
# Create a layout
9+
layout = pn.LinearLayout()
10+
11+
# Create a button and add it to layout
12+
button = pn.Button("Click Me")
13+
layout.add_view(button)
14+
15+
# Create a label and add it to layout
16+
label = pn.Label("Hello, World!")
17+
layout.add_view(label)
18+
19+
# Add layout to main view
20+
main_view.add_view(layout)
21+
22+
# Display the main view
23+
main_view.show()
24+
25+
26+
if __name__ == "__main__":
27+
main()

libs/pythonnative/pythonnative/pythonnative.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def set_title(self, title: str) -> None:
2626
def get_title(self) -> str:
2727
return self.native_instance.titleForState_(0)
2828

29+
2930
class Label:
3031
native_class = ObjCClass("UILabel")
3132

@@ -39,6 +40,21 @@ def set_text(self, text: str) -> None:
3940
def get_text(self) -> str:
4041
return self.native_instance.text()
4142

43+
44+
class LinearLayout:
45+
native_class = ObjCClass("UIStackView")
46+
47+
def __init__(self) -> None:
48+
self.native_instance = self.native_class.alloc().initWithFrame_(
49+
((0, 0), (0, 0)))
50+
self.native_instance.setAxis_(0) # Set axis to vertical
51+
self.views = []
52+
53+
def add_view(self, view):
54+
self.views.append(view)
55+
self.native_instance.addArrangedSubview_(view.native_instance)
56+
57+
4258
elif system == "Android":
4359
from java import jclass
4460

@@ -56,6 +72,7 @@ def set_title(self, title: str) -> None:
5672
def get_title(self) -> str:
5773
return self.native_instance.getText().toString()
5874

75+
5976
class Label:
6077
native_class = jclass("android.widget.TextView")
6178

@@ -69,5 +86,20 @@ def set_text(self, text: str) -> None:
6986
def get_text(self) -> str:
7087
return self.native_instance.getText().toString()
7188

89+
90+
class LinearLayout:
91+
native_class = jclass("android.widget.LinearLayout")
92+
93+
def __init__(self) -> None:
94+
self.native_instance = self.native_class()
95+
self.native_instance.setOrientation(
96+
1) # Set orientation to vertical
97+
self.views = []
98+
99+
def add_view(self, view):
100+
self.views.append(view)
101+
self.native_instance.addView(view.native_instance)
102+
103+
72104
else:
73105
raise PlatformNotDetectedError("Platform could not be detected or is unsupported.")

0 commit comments

Comments
 (0)