Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set layout size in Kivy

First of all, if they are some system differences, I work on Ubuntu 12.04, using current Kivy version. My problem is that i'm unable to set layout size.

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout

class TestApp(App):
    def build(self):
        layout = BoxLayout(orientation='vertical', size=(200,200))
        btn1 = Button(text='Hello', size=(50,50), size_hint=(None, None))
        btn2 = Button(text='World', size=(50,50), size_hint=(None, None))
        layout.add_widget(btn1)
        layout.add_widget(btn2)
        return layout
TestApp().run()

Any idea?

like image 502
user1433733 Avatar asked Sep 03 '12 13:09

user1433733


People also ask

Which class is used for defining layout in Kivy?

Python3. # base Class of your App inherits from the App class. # BoxLayout arranges children in a vertical or horizontal box.

What is Size_hint?

Understanding the size_hint Property in Widget ¶ The size_hint is a tuple of values used by layouts to manage the sizes of their children. It indicates the size relative to the layout's size instead of an absolute size (in pixels/points/cm/etc).


1 Answers

Root widget will always be the size of the window, you can change your code to:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout

class TestApp(App):
    def build(self):
        root = FloatLayout()
        layout = BoxLayout(orientation='vertical', size=(200,200), size_hint=(None, None))
        btn1 = Button(text='Hello', size=(50,50), size_hint=(None, None))
        btn2 = Button(text='World', size=(50,50), size_hint=(None, None))
        layout.add_widget(btn1)
        layout.add_widget(btn2)
        root.add_widget(layout)
        return root
TestApp().run()

But using a boxlayout and use custom size to all children seems a little weird.

like image 186
Tshirtman Avatar answered Nov 01 '22 15:11

Tshirtman