Search code examples
pythonkivykivy-language

Kivy ScrollView of Label won't scroll


I have a Label inside a ScrollView, hoping that that when the (multiline) text takes up too many lines, the user can scroll up & down, and when the lines are too long, the user can scroll left & right. And I can't get it to scroll. Here's the relevant part of my Builder string:

ScrollView:
    id: scrollLayout
    size_hint_y: 0.9
    Label:
        id: sortFilesDisplay
        size_hint_y: 0.9
        text: 'Drag\\n in\\n files\\n to\\n be\\n sorted\\n yea\\n go\\n now\\n testing\\n please\\n work\\n now\\help'

I've read the Kivy docs on ScrollView, that say I need to specify one of the size_hints to enable scrolling. I've readseveral SO posts about getting ScrollView to work:

but they all involve embedding some kind of Layout (e.g. GridLayout), abut I'm not embedding a layout, just one Label. I've tried setting minimum_height to various things I saw in those posts, but still no effect.

Those two size_hint_y's are in there just to try to do as directed; I don't need them. Also tried setting them to None.

Any ideas?

Also, that static string for text isn't what I ultimately want. I want the Label to remain scrollable when the string changes (when users drop in a new list of files), but I thought perhaps the 'dynamic' action was a problem, so for now I 'retreated' to trying a static string.


Solution

  • In order for the Label to be scrollable it should be bigger than ScrollView widget, so you need to set size_hint to None and bind label size to text size. For example:

    test.kv:

    ScrollView:
        Label:
            id: sortFilesDisplay
            size_hint: None, None     # <<<<<<<<<<
            size: self.texture_size   # <<<<<<<<<<
            text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit,\n sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n\n" * 20
    

    main.kv

    from kivy.app import App
    from kivy.config import Config
    Config.set('graphics', 'width', '300')
    Config.set('graphics', 'height', '200')
    
    
    class TestApp(App):
        pass
    
    
    if __name__ == '__main__':
        TestApp().run()