文本输入产生奇怪的缩进,而不是具有相同设置的标签

问题描述 投票:0回答:1

我试图将具有相同设置的文本输入和标签放在一个位置,以便文本位于同一位置,但文本输入会产生奇怪的缩进,而标签则不会。

我正在使用标签的函数

_label.texture.size
计算宽度有限的文本高度,然后将此高度和宽度应用于文本输入和标签,期望两个文本位于同一位置,以实现准确的标签文本选择。但我明白了,首先一切都很好,但随后文本输入中的文本会产生奇怪的缩进,然后所有文本都会出错。 结果: My problem

代码:


from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout

class main(App):

    def build(self):


        f = FloatLayout()




        text = "This is random text Every time you're online, you are bombarded by pictures, articles, links and videos trying to tell their story. Unfortunately, not all of these stories are true. Sometimes they want you to click on another story or advertisement at their own site, other times they want to upset people for political reasons. "
        labeltocheckheight = Label(text=text, text_size=(200, None), markup=True,font_size="24", size=(400, 100), pos=(100, 100), size_hint=(1, 1), valign='top')

        labeltocheckheight._label.refresh()
        width, height = labeltocheckheight._label.texture.size
        
        label = Label(text=text, text_size=(width, height), markup=True,font_size="24", size=(width, height), pos=(100, 100), size_hint=(None, None), valign='top')
        textinput2 = TextInput(text=text, font_size="24", size=(width, height), cursor=(0,0),pos=(100, 100), size_hint=(None, None), readonly=True, foreground_color=(1, 1, 1, 0.5),background_color=(1, 0, 0, 0), padding=0)


        f.add_widget(textinput2)
        f.add_widget(label)




        return f





main().run()
python kivy
1个回答
0
投票

Label
TextInput
使用不同的算法来换行文本,因此它们不太可能达成一致。您可以处理
labeltocheckheight
中的文本,提取各个行,然后将它们设置为
TextInput
的文本。这是使用此方法的
build()
方法的修改版本:

def build(self):
    f = FloatLayout()

    text = "This is random text Every time you're online, you are bombarded by pictures, articles, links and videos trying to tell their story. Unfortunately, not all of these stories are true. Sometimes they want you to click on another story or advertisement at their own site, other times they want to upset people for political reasons. "
    labeltocheckheight = Label(text=text, text_size=(200, None), markup=True,font_size="24", size=(400, 100), pos=(100, 100), size_hint=(1, 1), valign='top')

    labeltocheckheight._label.refresh()
    width, height = labeltocheckheight._label.texture.size

    text_for_ti = ''
    num_line = len(labeltocheckheight._label._cached_lines)
    for line_num in range(num_line):
        line = labeltocheckheight._label._cached_lines[line_num]
        if line_num == num_line - 1:
            text_for_ti += line.words[0].text
        else:
            text_for_ti += line.words[0].text + '\n'


    label = Label(text=text, text_size=(width, height), markup=True,font_size="24", size=(width, height), pos=(100, 100), size_hint=(None, None), valign='top')
    textinput2 = TextInput(text=text_for_ti, font_size="24", size=(width, height), cursor=(0,0),pos=(100, 100), size_hint=(None, None), readonly=True, foreground_color=(1, 1, 1, 0.5),background_color=(1, 0, 0, 0), padding=0)


    f.add_widget(textinput2)
    f.add_widget(label)

    return f

唯一的区别是

text_for_ti
的计算,以及将其用于
text
TextInput
属性。

© www.soinside.com 2019 - 2024. All rights reserved.