如何在Kivy中为动态创建的标签启用滚动视图?

时间:2019-11-06 02:34:01

标签: python kivy kivy-language

我的应用包含2个屏幕:

第一个屏幕:欢迎屏幕-显示欢迎消息的标签和进入下一个屏幕的按钮

第二个屏幕:动态创建的标签

我想在第二个屏幕上实现一个scrollview,其中包含动态创建的标签。我尝试过,但是scrollview无法正常工作。

以下是我的代码:

main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

class Welcome(Screen):
    pass

class Second(Screen):
    dynamic_labels = []    

    texts = ['Label '+str(i) for i in range(1,20)]
    no_widgets = len(texts)

    def set_data(self):
        for data,wid in zip(self.texts,self.dynamic_labels):
            wid.text = data

class WindowManager(ScreenManager):
    pass    

class SampleApp(App):   

    def build(self):
        return Builder.load_file("questapp\mytest.kv")

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

mytest.kv

#: import Label kivy.uix.label.Label

WindowManager:
    Welcome:
    Second:

<Welcome>
    name: "Welcomescreen"

    GridLayout:
        cols:1

        Label:
            text: "Welcome to my app"

        Button:
            text: "Proceed"
            on_press: 
                app.root.current = "screen2"

<Second>
    name: "screen2"

    FloatLayout:
        ScrollView:
            do_scroll_y: True
            do_scroll_x: True

            GridLayout:
                spacing:20
                padding: 20
                size_hint: 1,None
                height:500
                cols:1
                on_kv_post:
                    for i in range(root.no_widgets):root.dynamic_labels.append(Label(size_hint = (0.1,None),height = 30))

                    for wid in range (root.no_widgets):self.add_widget(root.dynamic_labels[wid])

                    root.set_data()

1 个答案:

答案 0 :(得分:0)

只需将GridLayout的{​​{1}}设置为height
您的self.minimum_height kv规则应为:

Second