标签上的gridlayout里面的kivy scrollview

时间:2017-11-06 00:42:35

标签: python kivy kivy-language

我尝试使用一个根规则集创建一个屏幕应用程序。通过按下按钮,我应该在按钮旁边的网格中看到可滚动的文本。在所有示例解决方案中,我看到scrollview正在使用我在其他问题中看到的类似KV文件。有人可以确定我在KV文件中遗漏了什么。

我的.py文件:

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()

我的KV文件:

#: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 ....."

我希望这不是重复。我们也欢迎任何其他代码建议。谢谢。

2 个答案:

答案 0 :(得分:1)

您没有设置Label的大小,因此默认值适用于任何小部件,默认值为

size: 100, 100
size_hint: 1, 1

由于size_hint1, 1,而父级是布局,size本身会被父级的可用空间覆盖

因为您在父版式中设置了size: minimum_size,所以它会给出其子项所需的最小尺寸,但Label不会要求任何空格,size_hint 1, 1意味着它很乐意占用所有可用空间。 Kivy通过不给它任何空间来解决这种情况,因此标签大小最终为0, 0,以及GridLayout的大小。

所以你要禁用size_hint(至少是Label的高度,而是将其设置为纹理高度

size_hint_y: None
height: self.texture_size[1]

通常可以通过设置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

答案 1 :(得分:-1)

你真的需要在scrollview中使用GridLayout吗?这对我有用:

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