KIVY标签内的文本

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

[尝试在矩形内放置文本。我什至尝试了here的解决方案。我希望文本在矩形内。即使我改变窗户的大小也应该很好。当我执行add_widget Actor时,我仍然想继续pos_hint和size_hint格式。任何想法请..

from kivy.uix.label import Label
from kivy.uix.relativelayout import RelativeLayout
from kivy.properties import ListProperty
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.scatter import Scatter
from kivy.core.window import Window

Window.clearcolor = (1, 1, 1, 1)
Window.size = (800, 600)

kv = '''
<Actor>:
    canvas:
        PushMatrix
        Color:
            rgba: 0,1,0,.8
        Rectangle:
            id: _rect_
            size: self.width, self.height/12
            pos: 0, 11 * (self.height / 12)
        Line:
            points: self.width/2, 11 * self.height/12, self.width/2, 0
            width:2
        PopMatrix
    Label:
        id: _actr_lbl
        text: 'Hello World'
        markup: True
        color: 0,0,0,1
        pos: 0, 11 * self.height/12
        halign: 'center'
'''

Builder.load_string(kv)

class Actor(Scatter):
    def __init__(self, **kwargs) :
        super(Actor, self).__init__(**kwargs)

class TestApp(App):
    def build(self):
        layout = RelativeLayout()
        layout.add_widget(Actor(pos_hint = {'center_x':0.5, 'top':0.95}, size_hint = (0.2, 1)))
        return layout

if __name__ == '__main__':
    TestApp().run()
label kivy
1个回答
0
投票

我认为您可以通过将kv稍微调整为Label来完成您想要的事情:

Label:
    id: _actr_lbl
    text: 'Hello World'
    markup: True
    color: 0,0,0,1
    size_hint: None, None
    size: root.width, root.height/12
    pos: 0, 11 * root.height/12
    halign: 'center'

添加size_hint: None, None使size生效。并且引用rootsize信息的pos可以保持Label的大小和正确的位置。

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