无法更改BoxLayout的高度

2 投票
1 回答
2288 浏览
提问于 2025-04-18 08:32

我想要改变一个嵌套在另一个BoxLayout里的BoxLayout的高度。我试过了十几种不同的组合,包括大小、大小提示和高度,但都没有效果。问题是,不管我怎么做,"ContainerBox"这个BoxLayout里面的BoxLayouts总是保持一样的大小。我该怎么改变嵌套BoxLayouts的高度呢?我想用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:

1 个回答

2

问题出在你使用了双重嵌套的布局上。举个例子,你有一个叫做 ContainerBox 的容器,它是一个 BoxLayout。然后你在里面添加了一个 Header,这个 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

撰写回答