python - KIVY : BoxLayout 包含水平 BoxLayout
问题描述
我试图将 4 个Horizontal
盒子布局以垂直方式堆叠在 BoxLayout 中。
我的 KV 文件:
<HBoxWidget>:
BoxLayout:
size: root.size
pos: root.pos
id: boxlayout_h
orientation: 'horizontal'
Image:
source: '/Users/driftking9987/Desktop/fp.gif'
<VBoxWidget1>:
BoxLayout:
spacing: 10
orientation: "horizontal"
size: [1,.25]
pos: root.pos
Label:
text: "Status : "
color: [0,84,80,19]
Label:
text: "Pending"
color: [0,84,80,19]
Widget:
<ContainerBox>:
orientation: 'horizontal'
HBoxWidget:
VBoxWidget1:
我计划以VBoxWidget
垂直方式拥有多个,但它只是行不通。
此外,为了实现[9] Label
,我想我会有BoxLayout
2 行,在水平方向,第 2 行将具有上述属性。但这只是行不通。下面是我得到的。我尝试将整个区域设置为 4 部分,但它没有给出预期的结果size_hint
。1,.25
PY 文件:
from kivy.app import App
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
class HBoxWidget(Widget):
def __init__(self, **kwargs):
super(HBoxWidget, self).__init__(**kwargs)
class VBoxWidget1(Widget):
def __init__(self, **kwargs):
super(VBoxWidget1, self).__init__(**kwargs)
class ContainerBox(BoxLayout):
def __init__(self, **kwargs):
super(ContainerBox, self).__init__(**kwargs)
class TestApp(App):
def build(self):
return ContainerBox()
if __name__ == '__main__':
TestApp().run()
解决方案
怎么样:我将 HBOX 和 VBOX 小部件更改为 BoxLayout,并将 Label 和另一个 BoxLayouts 添加到 ContainerBox。它看起来很像你的画
<HBoxWidget>:
AnchorLayout:
anchor_x: 'center'
anchor_y: 'center'
Image:
source: 'duck.jpg'
<VBoxWidget1>:
BoxLayout:
orientation: "horizontal"
size: [1,.25]
pos: root.pos
Label:
text: "Status : "
color: [0,84,80,19]
Label:
text: "Pending"
color: [0,84,80,19]
Widget: # Because of this widget Labels are not in the middle, its not on your drawing tough
<ContainerBox>:
orientation: 'vertical'
Label:
text: 'Label'
size_hint_y: 0.1
BoxLayout:
id: four_horizontals
orientation: 'horizontal'
HBoxWidget:
BoxLayout:
orientation:'vertical'
# One solution
#GridLayout:
# cols:1
# padding: 100
# VBoxWidget1:
# VBoxWidget1:
# VBoxWidget1:
# VBoxWidget1:
#Second Solution
BoxLayout:
#size_hint_y: 0 to 1 - it says how much you reduce height. Now its 1/3 of its parent, because there are 3 boxlayouts. if you set 0.5, it will have 1/3*0.5 and the other 2 boxlayouts takes the height which you took from this one
BoxLayout:
orientation:'vertical'
VBoxWidget1:
VBoxWidget1:
VBoxWidget1:
VBoxWidget1:
BoxLayout:
Python
from kivy.app import App
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
class HBoxWidget(BoxLayout):
def __init__(self, **kwargs):
super(HBoxWidget, self).__init__(**kwargs)
class VBoxWidget1(BoxLayout):
def __init__(self, **kwargs):
super(VBoxWidget1, self).__init__(**kwargs)
class ContainerBox(BoxLayout):
def __init__(self, **kwargs):
super(ContainerBox, self).__init__(**kwargs)
class TestApp(App):
def build(self):
return ContainerBox()
if __name__ == '__main__':
TestApp().run()
附加信息:
我管理这 4 个标签的方式并不是真正正确的方式。我会给你提示如何更正确地解决它。检查https://kivy.org/doc/stable/api-kivy.uix.floatlayout.html#module-kivy.uix.floatlayout
在 BoxLayout 上,小部件会自动组织自己 - 水平或垂直,它们会根据属于它们的空间大小进行缩放。使用 FloatLayout,没有任何限制,因此您可以随意放置标签。
一般来说,如果你有改变的分辨率,最好用 BoxLayouts 来解决。如果您想要更多自由,请使用 FloatLayout,但您必须自己管理小部件的缩放和定位
推荐阅读
- java - can't find the null pointer exception
- swift - How do i disable vertical scrolling but enable horizontal scrolling in collectionview swift 4?
- postgresql - 为 Postgres 分区创建自定义哈希运算符
- spring - 如果应用程序的 application.yaml 中存在配置组合,如何使 kotlin spring boot 应用程序失败
- sql-server - 从逗号分隔的字符串生成 XML + SQL Server
- mysql - 无法在 MySQL 中进行复杂查询
- c# - 多角色授权处理程序 - 仅获取方法角色
- mysql - 在 MySQL 数据库 8.0.17 中选择行日期之间的差异
- javascript - 如何通过内部键的值(在 JS 中)访问嵌套字典的对象?
- swift - 如何将 NSView 定位在另一个 NSView 下方?