Mittenchops
Mittenchops

Reputation: 19664

AnchorLayout not centering in Kivy

Following up on a question I asked about background canvases here, to make a nested layout described here, I have an AnchorLayout background that I'm nesting a relative layout on to form a margin around a fixed size internal float.

However, my nested widget is not centering inside my AnchorLayout, despite this both being the default behavior for an anchor layout, and also being explicitly declared. Why is this happening? My code is here:

#!/usr/bin/kivy
import kivy
kivy.require('1.7.2')

from random import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Color, Ellipse, Rectangle

class MinimalApp(App):
    title = 'My App'
    def build(self):
        root = RootLayout()
        return(root)

class RootLayout(AnchorLayout):
    pass

class Junk(RelativeLayout):
    pass

if __name__ == '__main__':
    MinimalApp().run()

And kv file:

#:kivy 1.7.2
#:import kivy kivy

<RootLayout>:
    anchor_x: 'center'                              # I think this /is/ centered
    anchor_y: 'center' 
    canvas.before:
        Color:
            rgba: 0.4, 0.4, 0.4, 1
        Rectangle:
            pos: self.pos
            size: self.size
    Junk:
        anchor_x: 'center'                          # this is /not/ centered.
        anchor_y: 'center' 
        Label:
            text: unicode(self.center)              # this /is/ appearing
            color: 1,0,1,1
            canvas.before:
                Color:
                    rgba: 0.94, 0.94, 0.94, 1
                Rectangle:
                    size: 400,400                   # this is /not/ centered
                    Label:
                        text: unicode(self.size)    # this is /not/ appearing
                        color: 1,0,0,1

Upvotes: 1

Views: 1425

Answers (1)

inclement
inclement

Reputation: 29460

            Rectangle:
                size: 400,400                   # this is /not/ centered
                Label:
                    text: unicode(self.size)    # this is /not/ appearing
                    color: 1,0,0,1

A Rectangle is a VertexInstruction, not a widget. VertexInstructions are their own thing, and are not sized or positioned by layouts. If you want them to have (for instance) the size and position of their parent widget, you must explicitly set this, as you do for an earlier Rectangle that does appear correctly.

edit: Likewise, canvas is not a widget, but refers to the canvas object that is a property of the widget. You cannot add widgets within it, only use graphics instructions.

For the Label that is not appearing, this is because VertexInstructions can't have children like widgets, so your kv language definition doesn't make sense. I'm surprised it doesn't throw an error, I guess it just fails silently instead.

Upvotes: 3

Related Questions