No se puede cambiar la altura de una BoxlLayout
Pregunta
Estoy tratando de cambiar la altura de un BoxLayout anidado dentro de otro BoxLayout.He probado más de una docena de combinaciones de tamaño, size_hints y la altura, pero nada parece funcionar.El problema es que, no importa lo que yo haga el BoxLayouts dentro de la "ContainerBox" BoxLayout, estos BoxLayouts son siempre del mismo tamaño.¿Cómo puedo cambiar las alturas de las BoxLayouts?Quiero usar el kv idioma para configurar las alturas no el código de 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 archivo
# 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:
Solución
Es debido a su doble anidada diseños.Por ejemplo, usted tiene ContainerBox
, que es un BoxLayout
.Así que a eso se le añaden Header
, que se extiende BoxLayout
pero tiene un size_hint
de (1, 1)
!Se establece la size_hint
en el BoxLayout
contenidos en el interior de Header
, pero que no afecta a la Header
sí.
Así que, para solucionar este problema:quitar la extra diseños anidados, así:
<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