【问题标题】:How to set a label position to border in Kivy?如何在 Kivy 中将标签位置设置为边框?
【发布时间】:2018-04-19 12:18:12
【问题描述】:

您好,我在 Kivy 中定位标签时遇到问题。 我认为图片最能描述我的问题。

现在的样子...

我希望它看起来像...

我想将标签 3 绑定到右边框。我不知道该怎么做。 我的代码:

import kivy

from kivy.app import App
from kivy.uix.label import Label

from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

root = Builder.load_string('''
Screen:
    BoxLayout:
        orientation:'vertical'
        Label:
            text: '1'
            font_size: self.height
            size_hint: (1.0, 0.17)
        Label:
            text: '2'
            font_size: self.height
            size_hint: (1.0, 0.83)
    Label:
        text: '3'
''')

class MyApp(App):
    def build(self):
        root.size_hint = (1.0, 1.0)
        return root

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

【问题讨论】:

    标签: python layout raspberry-pi kivy kivy-language


    【解决方案1】:

    对于这种类型的情况,有AnchorLayout,用于将小部件与相对位置对齐。

    import kivy
    
    from kivy.app import App
    from kivy.uix.label import Label
    
    from kivy.uix.boxlayout import BoxLayout
    from kivy.lang import Builder
    
    root = Builder.load_string('''
    Screen:
        BoxLayout:
            orientation:'vertical'
            Label:
                text: '1'
                font_size: self.height
                size_hint: (1.0, 0.17)
            Label:
                text: '2'
                font_size: self.height
                size_hint: (1.0, 0.83)
    
        AnchorLayout:
            anchor_x: 'right'
            anchor_y: 'bottom'
            Label:
                text: '3'
                font_size: self.height
                size_hint: None, None
    ''')
    
    class MyApp(App):
        def build(self):
            root.size_hint = (1.0, 1.0)
            return root
    
    if __name__ == '__main__':
        MyApp().run()
    

    【讨论】:

      【解决方案2】:

      您可能想使用FloatLayout。以下示例使用 FloatLayout 而不减小标签 ("2" - size_hint: 1, 0.83) 和 ("1" - size_hint: 1, 0.17) 的大小。

      示例

      main.py

      ​​>
      from kivy.app import App
      from kivy.lang import Builder
      
      root = Builder.load_string('''
      #:kivy 1.10.0
      
      Screen:
          FloatLayout:
              Label:
                  text: '1'
                  font_size: self.height
                  size_hint: (1, 0.17)
                  pos_hint: {'x': 0, 'y': 0.8}
      
              Label:
                  text: '2'
                  font_size: self.height
                  size_hint: (1.0, 0.83)
      
              Label:
                  text: '3'
                  font_size: self.height
                  size_hint: (1, 0.17)
                  pos_hint: {'x': 0.3, 'y': 0.1}
      ''')
      
      
      class MyApp(App):
      
          def build(self):
              root.size_hint = (1.0, 1.0)
              return root
      
      
      if __name__ == '__main__':
          MyApp().run()
      

      输出

      【讨论】:

        【解决方案3】:

        创建应用程序的古怪方式是将小部件放入小部件中。你应该尝试如下,我相信你能得到你想要的。

        root = Builder.load_string('''
        Screen:
            BoxLayout:
                orientation:'vertical'
                Label:
                    text: '1'
                    font_size: self.height
                    size_hint: (1.0, 0.17)
                BoxLayout:
                    Orientation: 'horizontal'
                    size_hint: (1.0, 0.83)
                    Label:
                        text: '2'
                        font_size: self.height
                        size_hint: (0.8, 1)
                    Label:
                        text: '3'
                        size_hint: (0.2, 1)
        ''')
        

        像这样,您的“3”仍将出现在屏幕中间。我想你可以用halign 来改变它,它应该在kivy 文档的某个地方。你也可以通过添加另一个BoxLayout 来解决这个问题,这次又是vertical

        【讨论】:

        • 感谢您的回答,在您的解决方案中,“2”标签不再居中。我认为我需要一种方法来设置不附加其他标签的“3”的位置。有没有办法将标签 3 设置为右下角的绝对位置?
        • 你可以按照 Eyllanesc 的描述做到这一点!锚布局将是要走的路。
        猜你喜欢
        • 2011-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多