1- # import pythonnative as pn
2- #
3- # button = pn.Button()
4- # label = pn.Label()
5-
61# Detect the platform
72import platform
83
@@ -21,10 +16,28 @@ class PlatformNotDetectedError(Exception):
2116 class Button :
2217 native_class = ObjCClass ("UIButton" )
2318
19+ def __init__ (self , title = "" ):
20+ self .native_instance = self .native_class .alloc ().init ()
21+ self .set_title (title )
22+
23+ def set_title (self , title ):
24+ self .native_instance .setTitle_forState_ (title , 0 ) # 0 is the state for 'normal'
25+
26+ def get_title (self ):
27+ return self .native_instance .titleForState_ (0 )
28+
2429 class Label :
2530 native_class = ObjCClass ("UILabel" )
2631
27- # Add more mappings here as required...
32+ def __init__ (self , text = "" ):
33+ self .native_instance = self .native_class .alloc ().init ()
34+ self .set_text (text )
35+
36+ def set_text (self , text ):
37+ self .native_instance .setText_ (text )
38+
39+ def get_text (self ):
40+ return self .native_instance .text ()
2841
2942elif system == "Android" :
3043 from java import jclass
@@ -33,10 +46,28 @@ class Label:
3346 class Button :
3447 native_class = jclass ("android.widget.Button" )
3548
49+ def __init__ (self , title = "" ):
50+ self .native_instance = self .native_class () # Instantiate the Java class
51+ self .set_title (title )
52+
53+ def set_title (self , title ):
54+ self .native_instance .setText (title )
55+
56+ def get_title (self ):
57+ return self .native_instance .getText ().toString ()
58+
3659 class Label :
3760 native_class = jclass ("android.widget.TextView" )
3861
39- # Add more mappings here as required...
62+ def __init__ (self , text = "" ):
63+ self .native_instance = self .native_class () # Instantiate the Java class
64+ self .set_text (text )
65+
66+ def set_text (self , text ):
67+ self .native_instance .setText (text )
68+
69+ def get_text (self ):
70+ return self .native_instance .getText ().toString ()
4071
4172else :
4273 raise PlatformNotDetectedError ("Platform could not be detected or is unsupported." )
0 commit comments