我想要一个具有日期的按钮,然后是一个天气图标,然后低于温度。但是当我这样做时,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字符然后将图标放在那些之上。我仍然想看看是否有更好的方法来实现这一点,因为它将在不同大小的移动设备上。
您必须使用带有多行标签的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()