Python/Kivy:动态行中的垂直滚动条无法正常工作

Python/Kivy : not working properly vertical scrollbar in dynamic row

我有两个文件 demo.pydemo.kv
我有一个添加行 dynamic.I 的按钮 +Add More 我正在尝试使用 ScrollView: 在动态行中添加垂直滚动条。但它无法正常工作。
这意味着当我在滚动视图中添加行时该行在滚动视图中有额外的 space 我想添加没有任何间距的行。

ScrollView:
            BoxLayout:
                orientation: "horizontal"
                size_hint_y: None
                height: 500

                Rows:
                    id: rows

demo.py

import kivy
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (500, 400)

class user(Screen):

    def add_more(self):
        self.ids.rows.add_row()


class Row(BoxLayout):
    button_text = StringProperty("")


class Rows(BoxLayout):
    orientation = "vertical"
    row_count = 0

    def __init__(self, **kwargs):
        super(Rows, self).__init__(**kwargs)
        self.add_row()

    def add_row(self):
        self.row_count += 1
        self.add_widget(Row(button_text=str(self.row_count)))


class Test(App):

    def build(self):
        self.root = Builder.load_file('demo.kv')
        return self.root


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

demo.kv

<Button@Button>:
    font_size: 15
    font_name: 'Verdana'

<Label@Label>:
    font_size: 15
    font_name: 'Verdana'

<TextInput@TextInput>:
    font_size: 15
    font_name: 'Verdana'
    padding_y: 3


<Row>:
    GridLayout:
        cols: 2
        row_force_default: True
        row_default_height: 40

        Button:
            text: root.button_text
            size_hint_x: None
            top: 200

        Button:
            text: 'World 1'
            width: 300


user:

    BoxLayout:
        orientation: "vertical"
        padding : 20, 5


        BoxLayout:
            orientation: "horizontal"
            #padding : 10, 10
            spacing: 10, 10
            size: 450, 40
            size_hint: None, None

            Label:
                size_hint_x: .2
                text: "Test 1"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

            Label:
                size_hint_x: .8
                text: "Test 2"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'


        ScrollView:
            BoxLayout:
                orientation: "horizontal"
                size_hint_y: None
                height: 500

                Rows:
                    id: rows


        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .2
            size_hint_y: .2

            Button:
                text: "+Add More"
                on_press: root.add_more()


        BoxLayout:
            orientation: "horizontal"
            padding : 10, 5
            spacing: 10, 10
            size_hint: .5, .35
            pos_hint: {'x': .25, 'y':.25}

            Button:
                text: 'Ok'

            Button:
                text: 'Cancel'

如有任何帮助,我们将不胜感激。

如果我明白你想要什么,你的嵌套太多了Layouts,那是不必要的。 Rows 应该是 ScrollView 的主要布局。

另一方面,Rows 应始终具有尽可能低的高度以包含其小部件 (minimun_height 属性),而不是固定尺寸。

Demo.py:

from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (500, 400)

class User(Screen):

    def add_more(self):
        self.ids.rows.add_row()


class Row(BoxLayout):
    button_text = StringProperty("")


class Rows(BoxLayout):
    row_count = 0

    def __init__(self, **kwargs):
        super(Rows, self).__init__(**kwargs)
        self.add_row()

    def add_row(self):
        self.row_count += 1
        self.add_widget(Row(button_text=str(self.row_count)))


class Test(App):

    def build(self):
        self.root = Builder.load_file('Demo.kv')
        return self.root


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

Demo.kv:

<Button@Button>:
    font_size: 15
    font_name: 'Verdana'

<Label@Label>:
    font_size: 15
    font_name: 'Verdana'

<TextInput@TextInput>:
    font_size: 15
    font_name: 'Verdana'
    padding_y: 3


<Row>:
    size_hint_y: None
    height: self.minimum_height
    height: 40

    Button:
        text: root.button_text
        size_hint_x: None
        top: 200

    Button:
        text: 'World 1'
        width: 300

<Rows>:
    size_hint_y: None
    height: self.minimum_height
    orientation: "vertical"

User:
    BoxLayout:
        orientation: "vertical"
        padding : 20, 5


        BoxLayout:
            orientation: "horizontal"
            #padding : 10, 10
            spacing: 10, 10
            size: 450, 40
            size_hint: None, None

            Label:
                size_hint_x: .2
                text: "Test 1"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

            Label:
                size_hint_x: .8
                text: "Test 2"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'


        ScrollView:
            Rows:
                id: rows


        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .2
            size_hint_y: .2

            Button:
                text: "+Add More"
                on_press: root.add_more()


        BoxLayout:
            orientation: "horizontal"
            padding : 10, 5
            spacing: 10, 10
            size_hint: .5, .35
            pos_hint: {'x': .25, 'y':.25}

            Button:
                text: 'Ok'

            Button:
                text: 'Cancel'