python - RecycleView 内的 Kivy 尺寸标签

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

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

如何使标签足够大,以便可以轻松阅读其内容?就像它已经适用于 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,所以我也感谢任何有关 RecycleView 的评论。

最佳答案

当标签值发生变化时,您必须绑定(bind)一个方法来更新 SnippetViewheight,因为您已在 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

...

关于python - RecycleView 内的 Kivy 尺寸标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47953132/

相关文章:

python - 使用 keras 序列模型的分类准确性较差

python - 写入现有的 xlsx 文件,仅覆盖 Python 中的一些工作表

python - 使用远程机器人在特定时间运行代码

python - Pyinstaller-python exe 停止工作 : "Cannot open self"

python - json 存储未正确更新值 kivy

python - 加速 python 3.5 循环以尽可能快地运行它

python - 如何防止 Docker 弄乱子进程输出顺序? (包括 MCVE)

python - 如何根据焦点设置Kivy TextInput背景颜色

python - kivy 应用程序未完成加载

python - 将 odbc 驱动程序安装到 azure 应用服务