我在Kivy中定位标签时遇到问题。 我认为图片可以最好地描述我的问题。
我想将Label 3绑定到右边框。我不知道该怎么做。 我的代码:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
root = Builder.load_string('''
Screen:
BoxLayout:
orientation:'vertical'
Label:
text: '1'
font_size: self.height
size_hint: (1.0, 0.17)
Label:
text: '2'
font_size: self.height
size_hint: (1.0, 0.83)
Label:
text: '3'
''')
class MyApp(App):
def build(self):
root.size_hint = (1.0, 1.0)
return root
if __name__ == '__main__':
MyApp().run()
答案 0 :(得分:3)
答案 1 :(得分:1)
您可能想要使用FloatLayout。以下示例使用 FloatLayout 而不减小标签的大小(" 2" - size_hint:1,0.83)和(" 1" - size_hint:1 ,0.17)。
from kivy.app import App
from kivy.lang import Builder
root = Builder.load_string('''
#:kivy 1.10.0
Screen:
FloatLayout:
Label:
text: '1'
font_size: self.height
size_hint: (1, 0.17)
pos_hint: {'x': 0, 'y': 0.8}
Label:
text: '2'
font_size: self.height
size_hint: (1.0, 0.83)
Label:
text: '3'
font_size: self.height
size_hint: (1, 0.17)
pos_hint: {'x': 0.3, 'y': 0.1}
''')
class MyApp(App):
def build(self):
root.size_hint = (1.0, 1.0)
return root
if __name__ == '__main__':
MyApp().run()
答案 2 :(得分:0)
创建应用程序的简单方法是将小部件放在小部件中。你应该尝试如下,我相信你将能够得到你想要的东西。
root = Builder.load_string('''
Screen:
BoxLayout:
orientation:'vertical'
Label:
text: '1'
font_size: self.height
size_hint: (1.0, 0.17)
BoxLayout:
Orientation: 'horizontal'
size_hint: (1.0, 0.83)
Label:
text: '2'
font_size: self.height
size_hint: (0.8, 1)
Label:
text: '3'
size_hint: (0.2, 1)
''')
像这样你的'3'仍然会在屏幕中间。我认为你可以用halign
改变它,它应该在kivy docs中的某个地方。您还可以通过添加其他BoxLayout
来解决此问题,这次又是vertical
。