[ How to align rectangle to center of parent with Kivy ]
I'm trying to draw a border around a FloatLayout
. To do this I'm adding two rectangles to the canvas, the second one slightly smaller than the layout of it's parent. This works. However, aligning a rectangle to the center of the canvas has proven elusive. The second rectangle needs to be centered to it's parent so that the slightly larger rectangle behind it shows through.
Things that do not work:
Rectangle:
size: (self.width*0.99, self.height*0.99)
center: self.center ## no property for Rect named center
Rectangle:
size: (self.width*0.99, self.height*0.99)
center_x: self.center_x ## no property for Rect named center_x/y
center_y: self.center_y
Rectangle:
size: (self.width*0.99, self.height*0.99)
pos: (self.pos.x, self.pos.y) ## can't reference x/y of self
Answer 1
I'm not sure what effect you're after, but either way you can just manage the offset manually. I've also added Color to distinguish the rectangles.
Here's an example with a 50 pixel offset:
Color:
rgba: 0, 1, 0, 1
Rectangle:
size: self.width, self.height
pos: self.pos
Color:
rgba: 1, 0, 0, 1
Rectangle:
size: self.width - 100, self.height - 100
pos: self.x + 50, self.y + 50
If you just want to draw a rectangular border and nothing else, you can just use a Line instead.
Answer 2
This also works.
Rectangle:
size: (self.width -4.0, self.height - 4.0)
pos: ((self.right - self.width + 2.0),(self.top - self.height + 2.0))