Resizing Boxlayout

Date: February 24th 2016
Last updated: February 24th 2016

A boxlayout is resized using size-hint.

size-hint is given equal share of the screen. A size-hint is added to the kv file like this: size-hint: 1, 1. If you turn off size hinting the element will be given a default size of 100 pixels.

main.py

import kivy
kivy.require('1.9.0')

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

class Sample(BoxLayout):
    pass

class SampleApp(App):
    def build(self):
        # return the boxlayout class defined above
        return Sample()

if __name__ == '__main__':
    SampleApp().run()

sample.kv

#:kivy 1.9.0
<Sample>:
    orientation: 'vertical'
    Label:
        text: 'Guess N sampling game!'
        size_hint: 1, None
    TextInput:
        text: 'Guess N...'
    BoxLayout:
        orientation: 'horizontal'
        size_hint: 1, None

        Button:
            text: 'Guess!'
            size_hint: 1, None
        Button:
            text: 'Clear'
            size_hint: None, None
        Button:
            text: 'Quit'
            size_hint: None, None

Output
Things to note:

  • Use size_hint to change layout:
  • Default equal share
  • size_hint: 1, 1 (100% width and 100% height)
  • size_hint: 1, None (turns height off)
  • Default height using None is 100px
  • size_hint: None, None (turns off % allocation -> 100 x 100 pixels)

resize boxlayout screenshot

results matching ""

    No results matching ""