【问题标题】:Multiple text fields inside of a Kivy language Button/LabelKivy 语言按钮/标签内的多个文本字段
【发布时间】:2019-04-13 14:36:53
【问题描述】:

我想要一个带有日期的按钮,然后在天气图标下方,然后在温度图标下方。但是当我这样做时,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 个标签(如下所示),它不会因 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 字符,然后将图标放在这些字符之上。我仍然想看看是否有更好的方法来做到这一点,因为它将在不同尺寸的移动设备上。

【问题讨论】:

    标签: kivy kivy-language


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      • 2014-12-26
      • 1970-01-01
      相关资源
      最近更新 更多