Advertisement
obernardovieira

Kivy basic build GUI

Aug 12th, 2016
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.91 KB | None | 0 0
  1. # Well, you can find a complete explanation at my wordpress page
  2. # http://obernardovieira.byethost18.com/kivy-para-ti-python-gui-framework/
  3. # it's in portuguese, but translators are available
  4. # and also check my POS (point of sale) project with kivy, on github (in english)
  5. # https://github.com/obernardovieira/POS-System
  6. # Thank you so much ;)
  7.  
  8.  
  9. ## USE .KV FILE TO BUILD A GUI
  10.  
  11. # test.py
  12. from kivy.app import App
  13.  
  14. class TestApp(App):
  15.     def build(self):
  16.         pass
  17.  
  18. TestApp().run()
  19.  
  20.  
  21.  
  22. # test.kv
  23. #:kivy 1.0
  24.  
  25. BoxLayout:
  26.  
  27.   Button:
  28.     text: 'Hello 1!'
  29.  
  30.   BoxLayout:
  31.     orientation: 'vertical'
  32.  
  33.     Button:
  34.       text: 'Hello 2!'
  35.     Button:
  36.       text: 'Hello 3!'
  37.  
  38.  
  39. ## USE ONLY PYTHON
  40.  
  41. # test.py
  42. from kivy.app import App
  43. from kivy.uix.boxlayout import BoxLayout
  44. from kivy.uix.button import Button
  45.  
  46. class TestApp(App):
  47.   def build(self):
  48.     mainWindow = BoxLayout()
  49.     mainWindow.add_widget(Button(text='Hello 1!'))
  50.     subWindow = BoxLayout(orientation='vertical')
  51.     subWindow.add_widget(Button(text='Hello 2!'))
  52.     subWindow.add_widget(Button(text='Hello 3!'))
  53.     mainWindow.add_widget(subWindow)
  54.     return mainWindow
  55.  
  56. TestApp().run()
  57.  
  58.  
  59. ### USER .KV FILE AND FINISH WITH PYTHON CODE (DYNAMIC WAY)
  60.  
  61. # test.py
  62. from kivy.app import App
  63. from kivy.uix.boxlayout import BoxLayout
  64. from kivy.uix.button import Button
  65. from kivy.properties import ObjectProperty
  66.  
  67. class Controller(BoxLayout):
  68.   subBox = ObjectProperty()
  69.  
  70.   def __init__(self, **kwargs):
  71.     super(Controller, self).__init__(**kwargs)
  72.     self.subBox.add_widget(Button(text='Hello 2!'))
  73.     self.subBox.add_widget(Button(text='Hello 3!'))
  74.    
  75. class TestApp(App):
  76.     def build(self):
  77.     return Controller()
  78.  
  79. TestApp().run()
  80.  
  81.  
  82. # test.kv
  83. #:kivy 1.0
  84.  
  85. <Controller>:
  86.   subBox: id_subBox
  87.  
  88.   BoxLayout:
  89.  
  90.     Button:
  91.       text: 'Hello 1!'
  92.     BoxLayout:
  93.       orientation: 'vertical'
  94.       id: id_subBox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement