Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you scroll a GridLayout inside Kivy ScrollView?

Tags:

python

kivy

At the moment this is my kv code that is not scrollable:

BoxLayout:
    id: bl
    orientation: 'vertical'
    padding: 10, 10
    row_default_height: '48dp'
    row_force_default: True
    spacing: 10, 10

    GridLayout:
        id: layout_content
        cols: 1
        row_default_height: '20dp'
        row_force_default: True
        spacing: 0, 0
        padding: 0, 0

        Label:
            text: 'You don''t have any downloads. Please add new download from Home screen'

How do you make the above kv code scrollable? I know that Kivy ScrollView only accept one child, and I have already make GridLayout to be child of a new ScrollView. But it's not working. Any suggestion?

like image 431
swdev Avatar asked Nov 01 '14 06:11

swdev


People also ask

How do I use scroll view in Kivymd?

You must carefully specify the size of your content to get the desired scroll/pan effect. By default, the size_hint is (1, 1), so the content size will fit your ScrollView exactly (you will have nothing to scroll). You must deactivate at least one of the size_hint instructions (x or y) of the child to enable scrolling.

What is GridLayout KIVY?

The GridLayout arranges children in a matrix. It takes the available space and divides it into columns and rows, then adds widgets to the resulting “cells”. Changed in version 1.0. 7: The implementation has changed to use the widget size_hint for calculating column/row sizes.


1 Answers

According to the documentation for ScrollView you have to disable at least one of the ScrollView's child size_hint:

<Controller>:
    layout_content: layout_content
    BoxLayout:
        id: bl
        orientation: 'vertical'
        padding: 10, 10
        row_default_height: '48dp'
        row_force_default: True
        spacing: 10, 10
        ScrollView:
            size: self.size
            GridLayout:
                id: layout_content
                size_hint_y: None
                cols: 1
                row_default_height: '20dp'
                row_force_default: True
                spacing: 0, 0
                padding: 0, 0

                Label:
                    text: "Lorem ipsum dolor sit amet"

And bind the layout's size to adapt itself:

# main.py

class Controller(FloatLayout):
    layout_content=ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Controller, self).__init__(**kwargs)
        self.layout_content.bind(minimum_height=self.layout_content.setter('height'))
like image 181
synw Avatar answered Oct 21 '22 17:10

synw