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

问题描述 投票:4回答:4

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

我想根据文本的数量调整gridlayout的标签或高度行的高度。

text label grid-layout kivy
4个回答
4
投票

虽然已经提出了解决方案,但我觉得他们没有利用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()

0
投票

qazxsw poi方法不改变Label的qazxsw poi属性。如果文字太长,它将与下面的内容重叠:

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

例:

height

0
投票

在thopiekar的帮助下,我找到了解决方案。

对于那些需要这个。到目前为止,我还没有找到没有这种方法的kivy

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

并且工作得很好!!!!!


0
投票

按照tshirtman的回答,我创建了在kv-lang中做同样事情的代码。由于您不需要分析回调函数,因此可能会更清楚一些情况。

发生的事情是标签的宽度根据布局设置,而高度设置为文本的纹理大小。因此,当文本字符串变长时,通过将boxlayout的高度设置为my_label.height,纹理只能在高度上增长,我们可以在上面应用。

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()
© www.soinside.com 2019 - 2024. All rights reserved.