ReivView内的Kivy尺码标签

时间:2017-12-23 14:03:25

标签: python python-3.x kivy kivy-language

enter image description here我希望我的标签具有显示其文本所需的高度。它在RecycleView中。我认为我的代码应该在RecycleView之外工作。

如何使Label足够大,以便可以轻松读取其内容?与CodeInput和TextInput的工作方式相同。

from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.label import Label
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import StringProperty


Builder.load_string('''
<RV>:
    viewclass: 'SnippetView'
    RecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        spacing: 10

<SnippetView>:
    canvas.before:
        Color:
            rgb: (255,0,0)
        Rectangle:
            pos: self.pos
            size: self.size
    orientation: 'vertical'

    Label:
        text: root.heading
        text_size: root.width, None
        size: self.texture_size
    TextInput:
        text: root.dscrpt
        size_hint_y: None
        height: self.minimum_height
    CodeInput:
        text: root.code
        size_hint_y: None
        height: self.minimum_height

''')

class SnippetView(BoxLayout):
    heading = StringProperty()
    dscrpt = StringProperty()
    code = StringProperty()


class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'heading': str(x),'dscrpt': str(x),'code': str(x)} for x in range(100)]

rv = RV()
runTouchApp(rv)

我也很感谢有关RecycleView的任何评论,因为我是第一次使用它。

1 个答案:

答案 0 :(得分:2)

当标签的值发生变化时,您必须绑定一个方法来更新height SnippetView,因为您已在RV的规则中修复了该标签。试试这个:

...

Builder.load_string('''

...

<SnippetView>:
    canvas.before:
        Color:
            rgb: (255,0,0)
        Rectangle:
            pos: self.pos
            size: self.size
    orientation: 'vertical'

    Label:
        id: l
        text: root.heading
        text_size: root.width, None
        size: self.texture_size
    TextInput:
        id: ti
        text: root.dscrpt
        size_hint_y: None
        height: self.minimum_height
    CodeInput:
        id: ci
        text: root.code
        size_hint_y: None
        height: self.minimum_height

''')

class SnippetView(BoxLayout):
    heading = StringProperty()
    dscrpt = StringProperty()
    code = StringProperty()

    def __init__(self, **kwargs):
        super(SnippetView, self).__init__(**kwargs)
        self.ids.l.bind(height=self.update)

    def update(self, *args):
        self.height = self.ids.l.texture_size[1] + self.ids.ti.height + self.ids.ci.height

...