Understanding Kivy's layouts

24 views
Skip to first unread message

Luigi Marongiu

unread,
Jun 11, 2019, 4:51:55 PM6/11/19
to Kivy users support
Hello,
I am a but confused with this Kivy language -- which I approach just last week.
On the manuals, it looks pretty simple, a bit like in HTML: the screen is divided in rows and columns and one populates the different cells sequentially. So I wrote the following scripts, with the idea of creating a screen with two rows one column, the first cell containing a button stratching 80% of the screen in the y dimension and 100% in the x dimension, the second cell containing a label of 20% on y and 100% on x:

# myapp.py
import kivy
kivy
.require('1.10.1')
from kivy.app import App
from kivy.uix.gridlayout import GridLayout


class myLayout(GridLayout):
   
pass


class MyApp(App):
   
def build(self):
       
return myLayout()


if __name__ == "__main__":
   
MyApp().run()


# myapp.kv
#:kivy 1.10.1
<myLayout@GridLayout>:
  orientation
: "vertical"
 
Button:
    size_hint_x
: 1
    size_hint_y
: 0.8
    pos_hint
: {'x': 0.5, 'top': 1}
    text
: "BUTTON HERE"
 
Label:
    size_hint_x
: 1
    size_hint_y
: 0.2
    pos_hint
: {'x': 0.5, 'top': 0}
    text
: "label here"


But the result is that both the button and the label are placed on the bottom left corner and are juxtaposed as in the attached figure.
What am I getting wrong?
Thank you
Screenshot from 2019-06-11 22-48-25.png

Jude Obande

unread,
Jun 11, 2019, 7:20:46 PM6/11/19
to Kivy users support
Quick fix. Use a different layout 


import kivy                                                                                    
from kivy.app import App                                                                        
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout


Builder.load_string('''
<Main>:
   BoxLayout:
       orientation: 'vertical'    
       Button:
           size_hint_y: 0.8
           text: 'Button'  
       Label:
           size_hint_y: 0.2
           text: 'Label'
''')    

class Main(BoxLayout):
   pass


class MainApp(App):
   def build(self):
       return Main()

MainApp().run()

Luigi Marongiu

unread,
Jun 12, 2019, 1:48:58 PM6/12/19
to kivy-...@googlegroups.com
Yep! Thank you!
> --
> You received this message because you are subscribed to the Google Groups "Kivy users support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
> To post to this group, send email to kivy-...@googlegroups.com.
> Visit this group at https://groups.google.com/group/kivy-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/8cdc47f7-3d35-4e63-98e4-404172e1b776%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Best regards,
Luigi
Reply all
Reply to author
Forward
0 new messages