Python / Kivy:在动态行中无法正常运行垂直滚动条

我有两个文件demo.py和demo.kv

我有一个按钮添加更多添加行动态.我正在尝试使用ScrollView在动态行中添加垂直滚动条:.但它无法正常工作.

它的意思是当我在scrollview中添加行时,在scrollview中有额外空间的行我想要添加没有任何间距的行.

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'

任何帮助将不胜感激.

最佳答案 如果我已经理解了你想要的东西,你就有太多嵌套的布局,这是不必要的.行应该是ScrollView的主要布局.

另一方面,行应始终具有最低可能的高度来包含其小部件(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'

《Python / Kivy:在动态行中无法正常运行垂直滚动条》

点赞