Kivy语言按钮/标签内的多个文本字段

时间:2018-11-10 02:58:14

标签: kivy kivy-language

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

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

然后,如果我尝试在Button内构建2个Label(如下所示),它将不会出现 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字符,然后在其上方放置了图标。我仍然想看看是否有更好的方法可以做到这一点,因为它将在不同大小的移动设备上使用。

1 个答案:

答案 0 :(得分:1)

您必须将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()
相关问题