Advertisement
SeriAce

Untitled

Apr 1st, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.uix.button import ButtonBehavior
  3. from kivy.uix.image import Image
  4. from kivy.uix.gridlayout import GridLayout
  5. from kivy.uix.scrollview import ScrollView
  6. from kivy.uix.label import Label
  7. from kivy.config import Config
  8. from kivy.graphics import Color, Rectangle
  9. from kivy.lang import Builder
  10. from kivy.properties import ObjectProperty
  11. from kivy.properties import StringProperty
  12. from kivy.properties import BooleanProperty
  13. import kivy.utils
  14.  
  15. Config.set('graphics', 'resizable', '0')
  16. Config.set('graphics', 'width', '540')
  17. Config.set('graphics', 'height', '960')
  18. Config.set('graphics', 'right', 1)
  19. Config.set('graphics', 'top', 1)
  20.  
  21. Builder.load_file("coinbar.kv")
  22. Builder.load_file("imagebutton.kv")
  23.  
  24.  
  25. class ImageButton(ButtonBehavior,Image):
  26. instance = StringProperty()
  27. am_i_selected = BooleanProperty()
  28. def __init__(self,**kwargs):
  29. super(ImageButton, self).__init__(**kwargs)
  30. # ---------- sets the image source ----------
  31. def on_instance(self, *args):
  32. pass
  33. if self.instance == "noteskeeper":
  34. self.am_i_selected = True
  35. else:
  36. self.am_i_selected = False
  37. #print(self.instance)
  38. #print(self.am_i_selected)
  39.  
  40. # ---------- on_press behavior of the coins ----------
  41. def select_coin(self):
  42. print(self.instance)
  43.  
  44. # ---------- NotesKeeper is selected at App start ----------
  45. selected = self.parent.coinbar_selected
  46. print(selected)
  47. if not self.am_i_selected:
  48. self.parent.ids[selected].am_i_selected = False
  49. self.parent.ids[selected].source = "cents/unselected/" + self.parent.ids[selected].instance + ".png"
  50. self.am_i_selected = True
  51. self.source = "cents/selected/" + self.instance + ".png"
  52. self.parent.coinbar_selected = self.instance
  53. return
  54. if self.am_i_selected:
  55. if self.instance == "noteskeeper":
  56. return
  57. else:
  58. self.am_i_selected = False
  59. self.source = "cents/unselected/" + self.instance + ".png"
  60.  
  61. # ---------- CoinBar ----------
  62. class CoinBar(GridLayout):
  63. def __init__(self,**kwargs):
  64. super(CoinBar, self).__init__(**kwargs)
  65. self.coinbar_selected = "noteskeeper"
  66. # ---------- Sets the CoinBar background color to white ----------
  67. with self.canvas.before:
  68. Color(rgb=(kivy.utils.get_color_from_hex("ffffff")))
  69. self.rect = Rectangle(size=self.size, pos=self.pos)
  70. self.bind(pos=self.update_bind_background, size=self.update_bind_background)
  71. def update_bind_background(self, *args):
  72. self.rect.pos = self.pos
  73. self.rect.size = self.size
  74.  
  75. # ---------- CatBar ----------
  76. class CatBar(GridLayout):
  77. cats = ["Reminder", "Idea", "Funny", "Well written"]
  78. def __init__(self,**kwargs):
  79. self.rows = 1
  80. super(CatBar, self).__init__(**kwargs)
  81. # ---------- creates the cats ----------
  82. for cat in self.cats:
  83. new_cat = ImageButton(instance=cat)
  84. self.add_widget(new_cat)
  85.  
  86. # ---------- Previews ----------
  87. class Previews(ScrollView):
  88. def __init__(self,**kwargs):
  89. super(Previews, self).__init__(**kwargs)
  90. # ---------- Middle Grid ----------
  91. noteskeeper_previews = NotesKeeperPreview()
  92. noteskeeper_previews.cols = 1
  93. x = ImageButton()
  94. x.source = "cents/NotesKeeper.png"
  95. noteskeeper_previews.add_widget(x)
  96.  
  97. # ----------- creation of the Previews ----------
  98. self.twocent_previews = GridLayout()
  99. self.twocent_previews.cols = 1
  100.  
  101.  
  102. self.add_widget(noteskeeper_previews)
  103.  
  104. # ---------- NotesKeeperPreview ----------
  105. class NotesKeeperPreview(GridLayout):
  106. def __init__(self, **kwargs):
  107. super().__init__()
  108. with self.canvas.before:
  109. Color(rgb=(kivy.utils.get_color_from_hex("ffffff")))
  110. self.rect = Rectangle(size=self.size, pos=self.pos)
  111. self.bind(pos=self.update_bind_background, size=self.update_bind_background)
  112.  
  113. def update_bind_background(self, *args):
  114. self.rect.pos = self.pos
  115. self.rect.size = self.size
  116.  
  117. # ---------- ButtonBar ----------
  118. class ButtonBar(GridLayout):
  119. def __init__(self, **kwargs):
  120. super(ButtonBar, self).__init__(**kwargs)
  121. self.rows = 1
  122. # ---------- sets background of the Button Bar to white ----------
  123. with self.canvas.before:
  124. Color(rgb=(kivy.utils.get_color_from_hex("ffffff")))
  125. self.rect = Rectangle(size=self.size, pos=self.pos)
  126. self.bind(pos=self.update_bind_background, size=self.update_bind_background)
  127.  
  128. # ---------- the buttons created at App startup ----------
  129. self.b1 = Label()
  130. self.b2 = Label()
  131. self.b3 = Label()
  132. self.b4 = Label()
  133.  
  134. # ---------- add_cat adds a new cat in the cat bar together with a note for that cat ----------
  135. self.add_cat = ImageButton(instance="add_cat")
  136. self.add_cat.on_press = self.plus_cat
  137.  
  138. # ---------- save_thought opens the NoteKeeping Widget for you to save a note in ----------
  139. self.save_thought = ImageButton(instance="save_thought")
  140. self.save_thought.on_press = self.plus_thought
  141.  
  142. self.b3 = self.add_cat
  143. self.b4 = self.save_thought
  144. self.add_buttons()
  145. # ---------- adds b1,b2,b3,b4 to the ButtonBar GridLayout ----------
  146. def add_buttons(self):
  147. self.add_widget(self.b1)
  148. self.add_widget(self.b2)
  149. self.add_widget(self.b3)
  150. self.add_widget(self.b4)
  151.  
  152. # ---------- future function to add a cat to the category bar ----------
  153. def plus_cat(self):
  154. print("This adds a cat")
  155.  
  156. # ---------- future function to add a thought to the previews ----------
  157. def plus_thought(self):
  158. print("this saves a thought")
  159.  
  160. # ---------- binds the background size and position of the background rectangle to the button bar size/pos ----------
  161. def update_bind_background(self, *args):
  162. self.rect.pos = self.pos
  163. self.rect.size = self.size
  164.  
  165. # ---------- MainScreen ----------
  166. class MainScreen(GridLayout):
  167. # ---------- Root of the main.kv grid. Has the coin_bar, cat_bar, previews and button_bar as children ----------
  168. def __init__(self,**kwargs):
  169. super(MainScreen, self).__init__(**kwargs)
  170.  
  171. # ---------- App Start ----------
  172. class MainApp(App):
  173. def __init__(self,**kwargs):
  174. super(MainApp, self).__init__(**kwargs)
  175.  
  176. def build(self):
  177. return MainScreen()
  178.  
  179.  
  180. MainApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement