/ui/intelli-sense-for-ui-objects.py

https://github.com/tdamdouni/Pythonista · Python · 85 lines · 60 code · 19 blank · 6 comment · 4 complexity · e9822adf7b1c90aa8a03a9da8e1bbef9 MD5 · raw file

  1. # https://forum.omz-software.com/topic/3486/intelli-sense-for-ui-objects
  2. '''
  3. Pythonista Forum - @Phuket2
  4. '''
  5. import ui
  6. import math
  7. class UIObject(object):
  8. def __init__(self, parent, ui_type = ui.Button, *args, **kwargs):
  9. self.target = ui_type()
  10. self.parent = parent
  11. self.target.ext = self
  12. self._rotate = 0
  13. self.do_kwargs(self.target, **kwargs)
  14. parent.add_subview(self.target)
  15. @staticmethod
  16. def do_kwargs(obj, **kwargs):
  17. for k, v in kwargs.items():
  18. if hasattr(obj, k):
  19. setattr(obj, k, v)
  20. @property
  21. def me(self):
  22. return self.target
  23. @property
  24. def center(self):
  25. self.target.center = self.parent.bounds.center()
  26. @property
  27. def rotate(self):
  28. return self._rotate
  29. @rotate.setter
  30. def rotate(self, angle):
  31. self.target.transform = self.rotatation_object(angle)
  32. self._rotate = angle
  33. @staticmethod
  34. def rotatation_object(angle):
  35. return ui.Transform.rotation(math.radians(angle))
  36. @staticmethod
  37. def get_ui_image(image_name):
  38. return ui.Image.named(image_name)
  39. def set_image(self, image_name):
  40. if not hasattr(self.target, 'image'):
  41. print('Object does not have a image attr...')
  42. return
  43. self.target.image = self.get_ui_image(image_name)
  44. def action_rotate_increment(self, sender):
  45. # just a stupid test
  46. self.rotate += 45
  47. class MyClass(ui.View):
  48. def __init__(self, *args, **kwargs):
  49. #super().__init__(*args, **kwargs)
  50. obj = UIObject(self, ui.Button, title = '', text_color ='deeppink')
  51. btn = obj.me
  52. btn.action = btn.ext.action_rotate_increment
  53. btn.ext.set_image('iob:arrow_up_a_256')
  54. btn.size_to_fit()
  55. btn.ext.center
  56. btn.ext.rotate = 90
  57. print(btn.ext.rotate)
  58. obj = UIObject(self, ui.Button, title = 'junk', text_color ='deeppink', border_width = .5)
  59. btn2 = obj.me
  60. btn2.size_to_fit()
  61. if __name__ == '__main__':
  62. _use_theme = False
  63. w, h = 600, 800
  64. f = (0, 0, w, h)
  65. mc = MyClass(frame=f, bg_color='white')
  66. mc.present('sheet', animated=False)