如何调整可变高度文本属性 kivy?

gqu*_*que 4 text label grid-layout kivy

我有一个很长的 kivy 文本。我想调整动态高度取决于文本的数量。

我的代码是这样的。

import kivy
from kivy.app import App 
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout


class DynamicHeight(App):y
    def build(self):
        grid = gl = GridLayout(cols=1)

        for i in range(3):
            l = Label(text='Text a longer line line line line line line line line', halign='left',text_size=(300, None))
            grid.add_widget(l)

        return grid

DynamicHeight().run()
Run Code Online (Sandbox Code Playgroud)

我想要根据文本量调整标签的高度或网格布局的高度行。

Tsh*_*man 5

虽然已经提出了解决方案,但我觉得他们没有利用 kivy 做事方式,而且这种方式更干净。您需要的是将 text_size 绑定到可用宽度,并将小部件的高度绑定到渲染的纹理大小。

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.floatlayout import FloatLayout


class MyApp(App):
    def build(self):
        root = FloatLayout()

        b = GridLayout(
            cols=1,
            pos_hint={
                'center_x': .5,
                'center_y': .5},
            size_hint=(None, None),
            spacing=20,
            width=200)
        b.bind(minimum_height=b.setter('height'))
        root.add_widget(b)

        for text_length in range(0, 80, 20):
            l = Label(
                text='word ' * text_length,
                size_hint_y=None)
            l.bind(width=lambda s, w:
                   s.setter('text_size')(s, (w, None)))
            l.bind(texture_size=l.setter('size'))
            b.add_widget(l)

        return root


if __name__ == '__main__':
    MyApp().run()
Run Code Online (Sandbox Code Playgroud)


Nyk*_*kin 0

text.size()方法不会更改height标签的属性。如果文字太长,它会与下面的内容重叠:

from kivy.app import App 
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout


class DynamicHeight(App):
    def build(self):
        layout = GridLayout(cols=1, spacing=20)

        l = Label(text='! '*100, text_size=(10, None),  size_hint_y=None, height=10)
        b = Button(text='...', size_hint_y=None)
        layout.add_widget(l)
        layout.add_widget(b)

        return layout

DynamicHeight().run()
Run Code Online (Sandbox Code Playgroud)

您需要计算文本的高度并手动设置height属性。我不知道有什么好的和干净的方法来做到这一点。这是一个肮脏的方法:

before = label._label.render()
label.text_size=(300, None)
after = label._label.render()
label.height = (after[1]/before[1])*before[1] # ammount of rows * single row height
Run Code Online (Sandbox Code Playgroud)

例子:

from kivy.app import App 
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView

class DynamicHeight(App):
    def build(self):
        layout = GridLayout(cols=1, size_hint_y=None, spacing=20)
        layout.bind(minimum_height=layout.setter('height'))
        for i in xrange(1, 20):
            l = Label(text='Text ' * (i*10), text_size=(300, None), size_hint_y=None)

            # calculating height here 
            before = l._label.render()
            l.text_size=(300, None)
            after = l._label.render()
            l.height = (after[1]/before[1])*before[1] # ammount of rows * single row height
            # end

            layout.add_widget(l)
        root = ScrollView()
        root.add_widget(layout)
        return root

DynamicHeight().run()
Run Code Online (Sandbox Code Playgroud)