我正在尝试更改嵌套在另一个 BoxLayout 内的 BoxLayout 的高度。我已经尝试了十多种尺寸、尺寸提示和高度的组合,但似乎没有任何效果。问题是无论我对“ContainerBox”BoxLayout 内部的BoxLayout 做什么,这些BoxLayout 的大小始终相同。如何更改嵌套 BoxLayout 的高度?我想使用 kv 语言来设置高度而不是 python 代码。
Python
import kivy
kivy.require('1.7.1')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.core.window import Window
Window.clearcolor = (1, 1, 1, 1)
class ContainerBox(BoxLayout):
def __init__(self, **kwargs):
super(ContainerBox, self).__init__(**kwargs)
class Header(BoxLayout):
def __init__(self, **kwargs):
super(Header, self).__init__(**kwargs)
class Toolbar(BoxLayout):
def __init__(self, **kwargs):
super(Toolbar, self).__init__(**kwargs)
class LoginScreen(GridLayout):
f_username = ObjectProperty(None)
f_password = ObjectProperty(None)
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
class LoginApp(App):
def build(self):
return ContainerBox()
if __name__ == '__main__':
LoginApp().run()
Kv文件
# kivy 1.7.1
<LoginScreen>:
f_username: username
f_password: password
GridLayout:
size: root.width, 200
pos: root.pos
row_default_height: 10
rows: 3
cols: 2
padding: 20
spacing: 10
Label:
text: 'User Name:'
color: 0.212, 0.486, 0.169, 1
font_size: 24
TextInput:
id: username
Label:
text: 'Password:'
color: 0.212, 0.486, 0.169, 1
font_size: 24
TextInput:
id: password
password: True
Label:
Button:
text: 'Login'
background_normal: 'white_20x20.png'
background_color: 0.212, 0.486, 0.169, 1
font_size: 24
<Header>:
BoxLayout:
orientation: 'vertical'
height: 100
size_hint_x: 1
size_hint_y: None
Label:
text: 'Dealer App'
color: 0.212, 0.486, 0.169, 1
font_size: 48
<Toolbar>
BoxLayout:
orientation: 'horizontal'
height: 36
size_hint_x: 1
size_hint_y: None
pos: root.pos
spacing: 5
Button:
text: 'One'
background_normal: 'white_20x20.png'
background_color: 0.212, 0.486, 0.169, 1
font_size: 16
Button:
text: 'Two'
background_normal: 'white_20x20.png'
background_color: 0.212, 0.486, 0.169, 1
Button:
text: 'Three'
background_normal: 'white_20x20.png'
background_color: 0.212, 0.486, 0.169, 1
Button:
text: 'Four'
background_normal: 'white_20x20.png'
background_color: 0.212, 0.486, 0.169, 1
<ContainerBox>:
BoxLayout:
orientation: 'vertical'
Header:
Toolbar:
LoginScreen:
最佳答案
这是由于您的双重嵌套布局造成的。例如,您有 ContainerBox
,它是一个 BoxLayout
。因此,您添加到 Header
,它扩展了 BoxLayout
,但 size_hint
为 (1, 1)
!您可以在 Header
内包含的 BoxLayout
上设置 size_hint
,但这不会影响 Header
本身。
因此,要解决此问题:删除额外的嵌套布局,如下所示:
<LoginScreen>:
f_username: username
f_password: password
size: root.width, 200
pos: root.pos
row_default_height: 10
rows: 3
cols: 2
padding: 20
spacing: 10
Label:
text: 'User Name:'
color: 0.212, 0.486, 0.169, 1
font_size: 24
TextInput:
id: username
Label:
text: 'Password:'
color: 0.212, 0.486, 0.169, 1
font_size: 24
TextInput:
id: password
password: True
Label:
Button:
text: 'Login'
background_normal: 'white_20x20.png'
background_color: 0.212, 0.486, 0.169, 1
font_size: 24
<Header>:
orientation: 'vertical'
height: 100
size_hint_x: 1
size_hint_y: None
Label:
text: 'Dealer App'
color: 0.212, 0.486, 0.169, 1
font_size: 48
<Toolbar>
orientation: 'horizontal'
height: 36
size_hint_x: 1
size_hint_y: None
pos: root.pos
spacing: 5
Button:
text: 'One'
background_normal: 'white_20x20.png'
background_color: 0.212, 0.486, 0.169, 1
font_size: 16
Button:
text: 'Two'
background_normal: 'white_20x20.png'
background_color: 0.212, 0.486, 0.169, 1
Button:
text: 'Three'
background_normal: 'white_20x20.png'
background_color: 0.212, 0.486, 0.169, 1
Button:
text: 'Four'
background_normal: 'white_20x20.png'
background_color: 0.212, 0.486, 0.169, 1
关于python - 无法更改 BoxlLayout 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24033977/