Kivy 语言中的多个文本字段 Button/Label

Multiple text fields inside of a Kivy language Button/Label

我想要一个带有日期的按钮,然后是天气图标下方,然后是温度图标下方。但是当我这样做时,kivy 似乎 ignore/overwrite 第一个文本字段和第二个文本字段:

Button:
    text: "Day"
    text_size: self.size
    halign: 'center'
    valign: 'top'
    padding_y: 10

    text: "temp"
    text_size: self.size
    halign: 'center'
    valign: 'bottom'
    padding_y: 30

    Image:
        source: "data/%s.png" % root.dIcon
        pos: self.parent.center_x-(self.width/2), self.parent.center_y-(self.height/4)
        height: self.parent.height-self.parent.height/3
        width: self.parent.width/2

然后,如果我尝试在按钮内构建 2 个标签(如下所示),它不会编译并出现 getitem 属性错误。

Button:
    Label:
        text: "Day"
        text_size: self.size
        halign: 'center'
        valign: 'top'
        padding_y: 10
    Label:
        text: "temp"
        text_size: self.size
        halign: 'center'
        valign: 'bottom'
        padding_y: 30
    Image:
        source: "data/%s.png" % root.dIcon
        pos: self.parent.center_x-(self.width/2), self.parent.center_y-(self.height/4)
        height: self.parent.height-self.parent.height/3
        width: self.parent.width/2

有没有办法做到这一点,或者我是否需要有单独的字段,例如只有图像可点击的 BoxLayout?

编辑: 我意识到我可以实现这一点,我在日期和温度之间放置了一些 \n 字符,然后将图标放在这些字符之上。我仍然想看看是否有更好的方法来执行此操作,因为它将在不同尺寸的移动设备上使用。

您必须使用带多行标签的 BoxLayout

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout

Builder.load_string('''
<MyButton>:
    orientation: 'vertical'
    Label: 
        text: "Day\nTemp"
        halign: 'center'
    Image:
        source: "data/%s.png" % root.dIcon
''')

class MyButton(BoxLayout, Button):
    pass

class TestApp(App):
    def build(self):
        return MyButton()

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

或带有 2 个标签的 BoxLayout:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout

Builder.load_string('''
<MyButton>:
    orientation: 'vertical'
    Label: 
        text: "Day"
    Label: 
        text: "Temp"
    Image:
        source: "data/%s.png" % root.dIcon
''')

class MyButton(BoxLayout, Button):
    pass

class TestApp(App):
    def build(self):
        return MyButton()

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