如何在Kivy中将标签位置设置为边框?

时间:2018-04-19 12:18:12

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

我在Kivy中定位标签时遇到问题。 我认为图片可以最好地描述我的问题。

现在看起来如何...... How it looks like now...

我希望它看起来像...... How I want it looks like...

我想将Label 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()

3 个答案:

答案 0 :(得分:3)

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

jenkins.war

enter image description here

答案 1 :(得分:1)

您可能想要使用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()

输出

Img01 - AppStartup

答案 2 :(得分:0)

创建应用程序的简单方法是将小部件放在小部件中。你应该尝试如下,我相信你将能够得到你想要的东西。

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 docs中的某个地方。您还可以通过添加其他BoxLayout来解决此问题,这次又是vertical