Search code examples
pythonkivyandroid-relativelayoutkivy-language

Relative layout position problem with pos


I hope for a hint about the position of the relative layout.

def new_game(self):
    box = BoxLayout(orientation='vertical', size_hint=(.7, .8), pos_hint=({'center_x': .5, 'center_y': .5}))
    left_box = BoxLayout(size_hint_x=.3)
    right_box = BoxLayout(size_hint_x=.7)
    top_box = BoxLayout(orientation='horizontal', size_hint_y=.85)
    bottom_box = RelativeLayout(size_hint_y=.15)
    bottom_box.add_widget(Button(size_hint=(.3, .7), pos_hint=({'center_x': .5, 'center_y': .5}),
                          on_release=lambda x: self.carousel.load_previous(), text='Начать игру'))
    top_box.add_widget(left_box)
    top_box.add_widget(right_box)
    box.add_widget(top_box)
    box.add_widget(bottom_box)
    return box

After adding a frame to the layer, I saw that the position was displaced.

<RelativeLayout>
    canvas.before:
        Color:
            rgba: 1, 1, 1, 1
        Line:
            width: 1
            rectangle: self.x, self.y, self.width, self.height

Here is image: Relative layout


Solution

  • The coordinates in a RelativeLayout are relative to the origin of the RelativeLayout. So when you draw a Rectangle at self.pos it is actually offset from the position of the RelativeLayout by whatever the self.pos vale is. You probably want:

    <RelativeLayout>
        canvas.before:
            Color:
                rgba: 1, 1, 1, 1
            Line:
                width: 1
                rectangle: 0, 0, self.width, self.height
    

    See the documentation.