简体   繁体   中英

kivy scrollview inside gridlayout on label

I trying to create a one screen app with one root rule set. by pressing a button i should see a scrollable text in the grid next to the button. In all the example solutions I see that scrollview is working in similar KV files that I have seen in other questions. Could someone please identify what I have missed in KV file.

my .py file:

import kivy
import string
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window

class RootContainer(BoxLayout):
    instance = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(RootContainer, self).__init__(**kwargs)

    def clickAction1(self, instance):
        #identify the button pressed
        buttonText = instance.text
        self.lbl1.text = instance.text + " some text goes here ... "
        myresult = " this is scrolling text.\n " * 30
        self.lbl5.text = myresult

class MBApp(App):
    def build(self):
        return RootContainer()


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

my KV file:

#:kivy 1.0.9
<RootContainer>:
    id: theRoot
    lbl1: my_labelC
    lbl5: my_labelCS
BoxLayout:
    orientation: 'vertical'
    spacing: 20
    padding: 20
    canvas: 
        Color: 
            rgb: 0, .33, 0 
        Rectangle: 
            pos: self.pos 
            size: self.size
    Button:
        text: "This is 1st button"
        text_size: self.size
        size_hint: (.5,1)
        on_press: theRoot.clickAction1(self)
    Button:
        text: "This is 2nd button"
        text_size: self.size
        size_hint: (.5,1)
        on_press: root.clickAction1(self)

GridLayout:
    rows: 2
    cols: 1
    spacing: 10
    padding: 10
    canvas: 
        Color: 
            rgb: .7, .63, 0  
        Rectangle: 
            pos: self.pos 
            size: self.size
    Label:
        id: my_labelC
        canvas.before:
            Color:
                rgb: 0,0,0
            Rectangle:
                pos: self.pos
                size: self.size
        text: "Header text for button clicked ......."
        text_size: self.size
    ScrollView:
        GridLayout:
            cols:1
            rows:1
            height: self.minimum_height
            Label:
                id: my_labelCS
                text: "Scrolling text goes here ....."

I hope this is not a duplicate. Any other suggestions to code are also welcome. Thank you.

You don't set the size of your Label, so the default applies, as any widget, the defaults are

size: 100, 100
size_hint: 1, 1

since size_hint is 1, 1 , and the parent is a layout, size itself is overriden by the parent's available space

since you set size: minimum_size in the parent layout, it'll give the minimum size its children require, but the Label doesn't ask for any space, it's size_hint of 1, 1 means that it's happy to take all the available space. Kivy solve this situation by not giving it any space, so the Label size ends up being 0, 0 , and the GridLayout's size as well.

So you want to disable size_hint (at least for the height, of the Label, and instead set it to the texture heights

size_hint_y: None
height: self.texture_size[1]

Which is usually sided with setting the texture width to the available width, by setting text_size.

text_size: self.width, None
size_hint_x: 1    # this is the default, so this line is not required, but you want to be sure not to disable this default

Do you really need the GridLayout inside your scrollview? This works for me:

ScrollView:
    Label:
        id: my_labelCS
        size_hint: 1, None
        text_size: self.width, None
        height: self.texture_size[1]
        text: "Scrolling text goes here ....."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM