首页 > 解决方案 > 如何使用 kivy 放置更好的尺寸和位置提示

问题描述

我目前正在开发一个用于移动设备的 kivy 应用程序。我有一些标签和按钮,如果我运行程序,它们的位置很好,但是当我缩小窗口时,标签和按钮中的文本会离开屏幕。任何人都可以提出更好的位置和大小提示,以便即使最小化窗口它们也不会离开屏幕?这是我的代码:

Label:
    color: 1,0,0,1
    pos_hint:{ "top":5,"left": 1}
    size_hint:1,1
    text: "Which number cannot be written in roman numbers?"
    font_size: 30
BoxLayout:
    Button:
        pos_hint:{"top": 1,"left": 1}
        size_hint:1,1
        text: "0"
        font_size: 30
        size: 30,30
        on_release:
            self.background_color = 250,255,0,0.3
    Button:
        pos_hint:{"top": 1,"left": 1}
        size_hint:1,1
        text: "1 million"
        font_size: 30
        size: 30,30
        on_release:
            self.background_color = 1, 0, 0, 1
BoxLayout:
    Button:
        pos_hint:{"top": 1,"left": 1}
        size_hint:1,1
        text: "999"
        font_size: 30
        size: 30,30
        on_release:
            self.background_color = 1, 0, 0, 1
    Button:
        pos_hint:{"top": 1,"left": 1}
        size_hint:1,1
        text: "10 million"
        font_size: 30
        size: 30,30
        on_release:
            self.background_color = 1, 0, 0, 1

标签: pythonandroidbuttonkivykivy-language

解决方案


四个你的Labeland Button,你可以只设置文本的大小,或者根据需要缩短文本或者向任意方向扩展,例如:

Label:
    text: 'this is some really long text which will be shortened to keep it inside the Label bounds'
    text_size: self.size
    shorten: True
    shorten_from: 'right'

您可以对Button. 我猜您可以做的另一件事是根据需要扩展Labelor Button,例如:

Label:
    text_size: [self.parent.width, None]
    size: self.texture_size

在这里,当文本的水平尺寸变得太大时,文本将简单地增长到新的一行


推荐阅读