3

My app contains 2 screens:

1st screen: welcome screen - A label to display welcome message and a button to proceed to next screen

2nd screen: Dynamically created labels

I would like to implement a scrollview to my second screen which contains dynamically created labels. I tried but the scrollview is not working.

Following is my code:

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()
Pavan
  • 381
  • 1
  • 4
  • 19
  • It's long to answer because we use BoxLayouts as containers and other techniques also rooting python and kivy files. Check this video https://youtu.be/i6nlVmWTyhg?t=265 and tell us if is what do you want. There's the tutorial https://www.youtube.com/watch?v=i6nlVmWTyhg&list=PLW062AfleDZbWPQXjyMeLOlcL8aQ4aLeP&index=1 – Pablo Díaz Nov 06 '19 at 05:28
  • Watched the youtube link but i want to know what modifications to be made in my code for the scrollview to work which is not shown in the video – Pavan Nov 06 '19 at 06:40

1 Answers1

0

Just set the GridLayout's height to self.minimum_height.
This is how your Second kv rule should be:

<Second>
    name: "screen2"

    FloatLayout:
        ScrollView:
            do_scroll_y: True
            do_scroll_x: True

            GridLayout:
                spacing:20
                padding: 20
                size_hint: 1,None
                # height: 500
                height: self.minimum_height
                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()
noEmbryo
  • 2,176
  • 2
  • 10
  • 15