简体   繁体   English

标签上gridlayout内的奇异滚动视图

[英]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. 在所有示例解决方案中,我都看到scrollview正在类似我在其他问题中看到的KV文件中工作。 Could someone please identify what I have missed in KV file. 有人可以在KV文件中找出我错过的内容。

my .py file: 我的.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()

my KV file: 我的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 ....."

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 由于size_hint1, 1并且父级为布局,因此size本身会被父级的可用空间覆盖

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. 由于您在父级布局中设置了size: minimum_size ,因此会给出其子级所需的最小大小,但是Label不要求任何空间,它的size_hint1, 1表示很高兴占用所有可用空间。 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. Kivy通过不给其任何空间来解决这种情况,因此Label的大小最终为0, 0 ,并且GridLayout的大小也是如此。

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(至少对于Label的高度而言),而是将其设置为纹理高度

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将纹理宽度设置为可用宽度。

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? 您是否真的需要在滚动视图内使用GridLayout? 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 ....."

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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