【问题标题】:KIVY Text within label标签内的 KIVY 文本
【发布时间】:2020-04-12 07:42:14
【问题描述】:

试图将文本放置在 Rectangle 中。我什至尝试了给定here 的解决方案。 我希望文本在矩形内。即使我请更改窗口大小,这也应该很好。当我添加_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】:

    我认为你可以通过将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 生效。并且为size 和pos 引用root 信息可以保持Label 的大小和位置正确。

    【讨论】:

      猜你喜欢
      • 2017-01-31
      • 2017-09-25
      • 2021-04-29
      • 1970-01-01
      • 2014-03-04
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多