Dirty Penguin
Dirty Penguin

Reputation: 4402

How to position BoxLayout to top of screen

I have a ActionBar that is nested inside a BoxLayout widget. I'd like the ActionBar to appear at the very top of the screen. Currently, I'm only able to get it to appear at the very bottom. Here is a screenshot:

enter image description here

Here is my main.py:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

class ShowActionBar(BoxLayout):
    pass

class MyTestApp(App):
    pass

if __name__ == "__main__":
    MyTestApp().run()

And here is my .kv file:

ShowActionBar:

<ShowActionBar>:
    orientation: "vertical"
    BoxLayout:
        pos_hint: {'top': 1}
        orientation: "vertical"
        ActionBar:
            ActionView:
                use_seperator: True
                ActionPrevious:
                    title: "My Title"
                    with_previous: False
                ActionOverflow:
                    ActionButton:
                        text: "Settings"
                ActionButton:
                    text: "Log Out"

As you can see, I've tried playing with various values for the pos_hint, but it does not appear to affect the position of the ActionBar. I suspect I'm missing something simple. What do I need to do to get the ActionBar to appear at the very top of the window?

Upvotes: 1

Views: 2745

Answers (1)

inclement
inclement

Reputation: 29450

Your example is actually a little too simple - you'll get the right behaviour as soon as you add another widget. For instance, here's a new rule for your widget:

<ShowActionBar>:
    orientation: "vertical"
    ActionBar:
        ActionView:
            use_seperator: True
            ActionPrevious:
                title: "My Title"
                with_previous: False
            ActionOverflow:
                ActionButton:
                    text: "Settings"
            ActionButton:
                text: "Log Out"
    Label:
        text: 'the next widget added fills all the extra space'
        text_size: self.size
        halign: 'center'
        valign: 'middle

Note that I also removed the unnecessary BoxLayout level.

The real problem is that BoxLayout ignores the pos_hint when it has just one child. Maybe it counts as a bug, but at best only a small one, since it isn't really intended to deal with a single child of fixed size - a FloatLayout would be more appropriate for this. However, a BoxLayout is probably exactly what you want once you do add more children.

Upvotes: 3

Related Questions