如何在 Kivy 中将标签位置设置为边框?
How to set a label position to border in Kivy?
您好,我在 Kivy 中定位标签时遇到问题。
我觉得图片最能说明我的问题。
现在的样子……
我多么希望它看起来像...
我想将标签 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()
kivy 创建应用程序的方式是将小部件放入小部件中。您应该按照以下方法尝试,我相信您一定能得到您想要的。
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 文档的某个地方。您也可以通过添加另一个 BoxLayout
来解决这个问题,这次又是 vertical
。
对于这种情况,有 AnchorLayout
,用于将小部件对齐到相对位置。
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)
AnchorLayout:
anchor_x: 'right'
anchor_y: 'bottom'
Label:
text: '3'
font_size: self.height
size_hint: None, None
''')
class MyApp(App):
def build(self):
root.size_hint = (1.0, 1.0)
return root
if __name__ == '__main__':
MyApp().run()
您可能想要使用 FloatLayout。以下示例使用 FloatLayout 而不减小标签的大小 ("2" - size_hint: 1, 0.83) 和 ("1" - size_hint: 1, 0.17).
例子
main.py
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()
输出
您好,我在 Kivy 中定位标签时遇到问题。 我觉得图片最能说明我的问题。
现在的样子……
我多么希望它看起来像...
我想将标签 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()
kivy 创建应用程序的方式是将小部件放入小部件中。您应该按照以下方法尝试,我相信您一定能得到您想要的。
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 文档的某个地方。您也可以通过添加另一个 BoxLayout
来解决这个问题,这次又是 vertical
。
对于这种情况,有 AnchorLayout
,用于将小部件对齐到相对位置。
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)
AnchorLayout:
anchor_x: 'right'
anchor_y: 'bottom'
Label:
text: '3'
font_size: self.height
size_hint: None, None
''')
class MyApp(App):
def build(self):
root.size_hint = (1.0, 1.0)
return root
if __name__ == '__main__':
MyApp().run()
您可能想要使用 FloatLayout。以下示例使用 FloatLayout 而不减小标签的大小 ("2" - size_hint: 1, 0.83) 和 ("1" - size_hint: 1, 0.17).
例子
main.py
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()