/kivy/kivypopup.py

https://github.com/Alamot/code-snippets · Python · 151 lines · 122 code · 27 blank · 2 comment · 12 complexity · 8ea7d1ef0d42a41d3da823c2a9bec44f MD5 · raw file

  1. #!/usr/bin/env python
  2. from kivy import platform, require
  3. require('1.9.0')
  4. from kivy.app import App
  5. from kivy.metrics import dp
  6. from kivy.core.window import Window
  7. from kivy.graphics import Color, Line, Rectangle
  8. from kivy.uix.widget import Widget
  9. from kivy.uix.button import Button
  10. from kivy.uix.label import Label
  11. from kivy.uix.modalview import ModalView
  12. from kivy.uix.boxlayout import BoxLayout
  13. from kivy.uix.scrollview import ScrollView
  14. BUTTON_TEXTURE = "atlas://data/images/defaulttheme/vkeyboard_background"
  15. class Pop(ModalView):
  16. def __init__(self, title, txt, callback=None, alpha=0.5,
  17. width=None, height=None, **kwargs):
  18. super(Pop, self).__init__(**kwargs)
  19. self.callback = callback
  20. self.auto_dismiss = False
  21. self.background = "dlgback_green.png"
  22. self.background_color = (0, 0, 0, alpha)
  23. self.size_hint = (None, None)
  24. self.preferred_width = width
  25. self.preferred_height = height
  26. if self.preferred_width:
  27. self.width = self.preferred_width
  28. elif Window.width > 500: # Big screen?
  29. self.width = 0.7*Window.width
  30. else:
  31. self.width = Window.width-2
  32. self.playout = BoxLayout(orientation='vertical',
  33. padding=["2dp", "5dp",
  34. "2dp", "5dp"],
  35. spacing="5dp")
  36. self.title = Label(size_hint_y=None,
  37. text_size=(self.width-dp(20), None),
  38. text=title,
  39. halign='left',
  40. font_size = "16sp",
  41. color=(0, 1, 1, 1),
  42. markup=True)
  43. self.separator = BoxLayout(size_hint_y=None, height="1dp")
  44. self.pscroll = ScrollView(do_scroll_x=False)
  45. self.content = Label(size_hint_y=None,
  46. text=txt,
  47. halign='justify',
  48. font_size="16sp",
  49. markup=True,
  50. text_size=(self.width-dp(20), None))
  51. self.pbutton = Button(text='Close',
  52. size_hint_y=None, height="25dp",
  53. background_normal=
  54. "atlas://data/images/defaulttheme/vkeyboard_background")
  55. self.pbutton.bind(on_release=self.close)
  56. self.add_widget(self.playout)
  57. self.playout.add_widget(self.title)
  58. self.playout.add_widget(self.separator)
  59. self.playout.add_widget(self.pscroll)
  60. self.pscroll.add_widget(self.content)
  61. self.playout.add_widget(self.pbutton)
  62. self.title.bind(texture_size=self.update_height)
  63. self.content.bind(texture_size=self.update_height)
  64. with self.separator.canvas.before:
  65. Color(0, 0.7, 0, 1)
  66. self.rect = Rectangle(pos=self.separator.pos,
  67. size=self.separator.size)
  68. self.separator.bind(pos=self.update_sep,
  69. size=self.update_sep)
  70. Window.bind(size=self.update_width)
  71. self.open()
  72. def update_width(self, *args):
  73. # hack to resize dark background on window resize
  74. self.center = Window.center
  75. self._window = None
  76. self._window = Window
  77. if self.preferred_width:
  78. self.width = self.preferred_width
  79. elif Window.width > 500: # Big screen?
  80. self.width = 0.7*Window.width
  81. else:
  82. self.width = Window.width-2
  83. self.title.text_size = (self.width - dp(20), None)
  84. self.content.text_size = (self.width - dp(20), None)
  85. def update_height(self, *args):
  86. self.title.height = self.title.texture_size[1]
  87. self.content.height = self.content.texture_size[1]
  88. temp = self.title.height+self.content.height+dp(56)
  89. if self.preferred_height:
  90. self.height = self.preferred_height
  91. elif temp > Window.height-dp(40):
  92. self.height = Window.height-dp(40)
  93. else:
  94. self.height = temp
  95. self.center = Window.center
  96. def update_sep(self, *args):
  97. self.rect.pos = self.separator.pos
  98. self.rect.size = self.separator.size
  99. def close(self, instance):
  100. self.dismiss(force=True)
  101. if self.callback:
  102. self.callback()
  103. class TestApp(App):
  104. def build(self):
  105. return Pop("Title",
  106. "Lorem ipsum dolor sit amet, "
  107. "ad solum soleat civibus pri, "
  108. "te natum ceteros sea. "
  109. "Et his nonumy nonumes. "
  110. "Diam cotidieque te has, nostro "
  111. "epicurei maluisset est at. "
  112. "Dicat scripserit at usu. "
  113. "Ne homero labore signiferumque vim, "
  114. "et qui petentium "
  115. "persequeris, pri at erant epicurei. "
  116. "Eu duo wisi causae, "
  117. "eum te nullam causae. "
  118. "Iudicabit scripserit id vim.",
  119. self.callback, alpha=0.5,
  120. width=None, height=None)
  121. def callback(self):
  122. exit()
  123. if __name__ == "__main__":
  124. TestApp().run()