Nice. Here is a slightly different approach.
- Rather than using new set of API, pythonista API is used as standard. For example, "example1.py" (modified example of yours) runs directly in pythonista (currently it requires one line change from kivy code)
- In Kivy, you need an extra file "ui.py". All the code is in this file. It is just a wrapper on kivy objects.
- This code is not complete and a lot needs to be done.
import ui
def closepage(sender):
v.close()
def function_1(sender):
v['label1'].text = 'Oh! You clicked my button.'
#uncomment or comment lines below based on platform
#v = ui.MainView(frame=(0, 0, 600, 450)) # kivy
v = ui.View(frame=(0, 0, 600, 450)) # pythonista
v.add_subview(ui.Label(frame=(80, 10, 240, 20),
name='label1',
text='Hey I am just a simple label.'))
v.add_subview(ui.Button(frame=(40, 40, 100, 40),
title='Click me',
action=function_1))
v.add_subview(ui.Button(frame=(460, 40, 100, 40),
title='Close me',
action=closepage))
v.add_subview(ui.TextField(frame=(40, 120, 300, 40),
name='textfield1',
text='I am a text field'))
v.add_subview(ui.ImageView(frame=(460, 310, 100, 100),
image=ui.Image('insidelogo.png')))
v.present('sheet')
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
from kivy.base import runTouchApp
from kivy.utils import platform as core_platform
import sys
from kivy.uix.label import Label as KivyLabel
from kivy.uix.button import Button as KivyButton
from kivy.uix.textinput import TextInput
from kivy.uix.image import Image as KivyImage
from kivy.graphics import Color
from kivy.graphics import Rectangle
class View(object):
screen_size = (800, 600)
xratio = screen_size[0] / 800.0
yratio = screen_size[1] / 600.0
def __init__(self, frame=(0,0,100,100),
name=''):
self.frame = frame
self.name = name
self.superview = None
self.uniobject = None
self.rootdict = {}
class MainView(View):
def __init__(self, frame=(0,0,100,100), name=''):
super().__init__(frame=frame, name=name)
self.root = FloatLayout()
Window.size = View.screen_size
self.root.canvas.add(
Color(1.0, 1.0, 1.0))
self.root.canvas.add(
Rectangle(pos = (self.frame[0] * View.xratio,
self.frame[1]*View.yratio),
size = (frame[2] * View.xratio,
frame[3] * View.yratio)))
def add_subview(self, v):
v.superview = self
if v.name:
self.rootdict[v.name] = v.kivyobject
self.root.add_widget(v.kivyobject)
def __getitem__(self, key):
return self.rootdict[key]
def close(self):
if core_platform == 'android':
sys.exit()
else:
Window.close()
def present(self, style):
#todo: screen size, style
runTouchApp(self.root)
class Button(View):
def __init__(self, frame=(0,0,100,100),
title='',
name='',
font=('Helvetica', 20),
action=None):
super().__init__(frame=frame, name=name)
self.kivyobject = (KivyButton(text=title,
size_hint_y = None,
size_hint_x = None,
width = self.frame[2]* View.xratio,
height = self.frame[3]* View.yratio,
pos = (self.frame[0] * View.xratio,
self.frame[1] * View.yratio),
on_press = action))
class Label(View):
def __init__(self, frame=(0,0,100,100),
text='',
alignment='',
name='',
font=('Helvetica', 20),
text_color='blue'):
super().__init__(frame=frame, name=name)
label = KivyLabel(text=text,
id=name,
size_hint=(1.0, 1.9),
halign="left",
valign="bottom",
pos = (self.frame[0] * View.xratio,
self.frame[1] * View.yratio))
#label.bind(size=label.setter('text_size'))
self.kivyobject = (label)
class TextField(View):
def __init__(self,
frame=(0,0,100,100),
text='',
name='',
font=('Helvetica', 20),
alignment='',
text_color='blue'):
#action=None):
super().__init__(frame=frame, name=name)
self.kivyobject = (TextInput(text=text,
size_hint_y = None,
size_hint_x = None,
height = self.frame[3]* View.yratio,
width = self.frame[2]* View.xratio,
multiline = True,
pos = (self.frame[0] * View.xratio,
self.frame[1] * View.yratio)))
#on_press = action))
class ImageView(View):
def __init__(self,
frame=(0,0,100,100),
name='',
image=None):
super().__init__(frame=frame, name=name)
if image:
image_source = image.source
else:
image_source = None
self.kivyobject = (
KivyImage(source=image_source,
allow_stretch = True,
size = (self.frame[2]* View.xratio,
self.frame[3]* View.yratio),
pos = (self.frame[0] * View.xratio,
self.frame[1] * View.yratio)))
class Image(object):
def __init__(self, source):
self.source = source
Copy of the code in gist.
https://gist.github.com/balachandrana/4e9accfc894785682f230c54dc5da816#file-ui-py