在 kivy 的 ScrollView 中将文本与 Label 的边缘对齐

Align text to edge of Label in ScrollView in kivy

我正在尝试使 Label 水平滚动,并希望 halign:"rightvalign:middle 按照下面的代码

ScrollView:

  Label:    
    id:maindisplay
    text:"0"
    font_size:"50sp"
    text_size: None ,self.height[1] #  Set the text wrap box height
    size_hint_x:None
    width: self.texture_size[0]

#Following part is not working
    halign: 'right'
    valign: 'middle'

这里的关键是文字的宽度。如果设置为None,则不受约束,如果文本比parent宽,则可以滚动。但是,如果文本只需要父级宽度的一小部分,则文本将在标签内居中(尽管多行仍将右对齐)。另一方面,如果 text_size 静态设置为有限值,不适合的文本将换行并且永远不会滚动。 hacky 解决方案是设置 text_size: 8000, self.height (范围很广,不太可能发生)。这允许滚动,但是很丑陋(并且可能会不小心滚动到没有显示任何内容的区域)。

相反,我们动态更改 text_width。当 text 改变时,我们首先移除宽度约束。然后我们让标签更新其内容,并将 text_size 设置为适合文本的宽度,但至少与父级一样宽。

其他设置确保标签本身至少与父标签一样宽 (width) 并且 ScrollView 从其右边缘开始 (scroll_x:1)

这是一个完整的例子:

from kivy.app import App
from kivy.lang import Builder
from kivy.config import Config
from kivy.clock import Clock
from kivy.properties import StringProperty, NumericProperty

kv = '''ScrollView:
  scroll_x: 1
  Label:
    canvas.before:
      Color:
        rgba:1,0,0,1
      Rectangle:
        pos: self.parent.pos
        size: self.size
    id:maindisplay
    text:"2"
    font_size:"50sp"
    text_size: None, self.height
    size_hint_x: None
    width:  max(self.texture_size[0], self.parent.width)
    halign: 'right'
    valign: 'middle'
    on_text: app.txt_cb(*args)
'''

class QApp(App):
    txt = StringProperty("1")
    num = NumericProperty(0)

    def add_string(self, dt=0):
        self.num += 1
        self.txt += "%i" % self.num
        self.root.ids.maindisplay.text = self.txt

    def txt_cb(self, obj, text):
        obj.text_size = (None, obj.height)
        obj.texture_update()
        obj.text_size = (max(obj._label.content_width, obj.parent.width), obj.height)

    def build(self):
        Clock.schedule_once(lambda dt:self.txt_cb(self.root.ids.maindisplay, ""), 0)
        Clock.schedule_interval(self.add_string, .5)
        return Builder.load_string(kv)

if __name__ == '__main__':
    QApp().run()