无法更改Boxlayout的高度

时间:2014-06-04 09:39:52

标签: python kivy

我试图改变嵌套在另一个BoxLayout内部的BoxLayout的高度。我已经尝试了十几种尺寸,尺寸和高度的组合,但似乎没有任何效果。问题在于我无论在盒子里面做了什么BoxLayouts" 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 个答案:

答案 0 :(得分:1)

这是由于你的双嵌套布局。例如,您有ContainerBox,即BoxLayout。因此,您添加Header BoxLayout,其size_hint延伸(1, 1) size_hint!您在BoxLayout内的Header上设置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 本身。

因此,要解决此问题:删除额外的嵌套布局,如下所示:

{{1}}
相关问题