/_2016/ui-kivy.py

https://github.com/tdamdouni/Pythonista · Python · 168 lines · 126 code · 22 blank · 20 comment · 5 complexity · 70c880b30396bf88ad59c3c16851087d MD5 · raw file

  1. # https://gist.github.com/balachandrana/4e9accfc894785682f230c54dc5da816#file-ui-py
  2. # https://forum.omz-software.com/topic/3964/unipage-as-a-bridge-between-kivy-and-pythonista/5
  3. from kivy.uix.floatlayout import FloatLayout
  4. from kivy.core.window import Window
  5. from kivy.base import runTouchApp
  6. from kivy.utils import platform as core_platform
  7. import sys
  8. from kivy.uix.label import Label as KivyLabel
  9. from kivy.uix.button import Button as KivyButton
  10. from kivy.uix.textinput import TextInput
  11. from kivy.uix.image import Image as KivyImage
  12. from kivy.graphics import Color
  13. from kivy.graphics import Rectangle
  14. #class View(object):
  15. # screen_size = (800, 600)
  16. # xratio = screen_size[0] / 800.0
  17. # yratio = screen_size[1] / 600.0
  18. # def __init__(self, frame=(0,0,100,100),
  19. # name=''):
  20. # self.frame = frame
  21. # self.name = name
  22. # self.superview = None
  23. # self.uniobject = None
  24. # self.rootdict = {}
  25. class View(object):
  26. screen_size = (800, 600)
  27. xratio = screen_size[0] / 800.0
  28. yratio = screen_size[1] / 600.0
  29. def __init__(self, frame=(0, 0, 100, 100), name=''):
  30. self.frame = frame
  31. self.name = name
  32. self.superview = None
  33. self.uniobject = None
  34. self.rootdict = {}
  35. @property
  36. def kivy_pos(self):
  37. """Convert a Pythonista ui frame into a Kivy x, y position"""
  38. x, y, _, _ = self.frame
  39. return x * self.xratio, y * self.yratio
  40. @property
  41. def kivy_size(self):
  42. """Convert a Pythonista ui frame into a Kivy width, height size"""
  43. _, _, w, h = self.frame
  44. return w * self.xratio, h * self.yratio
  45. @property
  46. def kivy_rect(self):
  47. """Convert a Pythonista ui frame into a Kivy Rectangle"""
  48. return Rectangle(pos=self.kivy_pos, size=self.kivy_size)
  49. class MainView(View):
  50. def __init__(self, frame=(0,0,100,100), name=''):
  51. super().__init__(frame=frame, name=name)
  52. self.root = FloatLayout()
  53. Window.size = View.screen_size
  54. self.root.canvas.add(
  55. Color(1.0, 1.0, 1.0))
  56. self.root.canvas.add(
  57. Rectangle(pos = (self.frame[0] * View.xratio,
  58. self.frame[1]*View.yratio),
  59. size = (frame[2] * View.xratio,
  60. frame[3] * View.yratio)))
  61. def add_subview(self, v):
  62. v.superview = self
  63. if v.name:
  64. self.rootdict[v.name] = v.kivyobject
  65. self.root.add_widget(v.kivyobject)
  66. def __getitem__(self, key):
  67. return self.rootdict[key]
  68. def close(self):
  69. if core_platform == 'android':
  70. sys.exit()
  71. else:
  72. Window.close()
  73. def present(self, style):
  74. #todo: screen size, style
  75. runTouchApp(self.root)
  76. class Button(View):
  77. def __init__(self, frame=(0,0,100,100),
  78. title='',
  79. name='',
  80. font=('Helvetica', 20),
  81. action=None):
  82. super().__init__(frame=frame, name=name)
  83. self.kivyobject = (KivyButton(text=title,
  84. size_hint_y = None,
  85. size_hint_x = None,
  86. width = self.frame[2]* View.xratio,
  87. height = self.frame[3]* View.yratio,
  88. pos = (self.frame[0] * View.xratio,
  89. self.frame[1] * View.yratio),
  90. on_press = action))
  91. class Label(View):
  92. def __init__(self, frame=(0,0,100,100),
  93. text='',
  94. alignment='',
  95. name='',
  96. font=('Helvetica', 20),
  97. text_color='blue'):
  98. super().__init__(frame=frame, name=name)
  99. label = KivyLabel(text=text,
  100. id=name,
  101. size_hint=(1.0, 1.9),
  102. halign="left",
  103. valign="bottom",
  104. pos = (self.frame[0] * View.xratio,
  105. self.frame[1] * View.yratio))
  106. #label.bind(size=label.setter('text_size'))
  107. self.kivyobject = (label)
  108. class TextField(View):
  109. def __init__(self,
  110. frame=(0,0,100,100),
  111. text='',
  112. name='',
  113. font=('Helvetica', 20),
  114. alignment='',
  115. text_color='blue'):
  116. #action=None):
  117. super().__init__(frame=frame, name=name)
  118. self.kivyobject = (TextInput(text=text,
  119. size_hint_y = None,
  120. size_hint_x = None,
  121. height = self.frame[3]* View.yratio,
  122. width = self.frame[2]* View.xratio,
  123. multiline = True,
  124. pos = (self.frame[0] * View.xratio,
  125. self.frame[1] * View.yratio)))
  126. #on_press = action))
  127. class ImageView(View):
  128. def __init__(self,
  129. frame=(0,0,100,100),
  130. name='',
  131. image=None):
  132. super().__init__(frame=frame, name=name)
  133. if image:
  134. image_source = image.source
  135. else:
  136. image_source = None
  137. self.kivyobject = (
  138. KivyImage(source=image_source,
  139. allow_stretch = True,
  140. size = (self.frame[2]* View.xratio,
  141. self.frame[3]* View.yratio),
  142. pos = (self.frame[0] * View.xratio,
  143. self.frame[1] * View.yratio)))
  144. class Image(object):
  145. def __init__(self, source):
  146. self.source = source