KSully2
KSully2

Reputation: 149

kivy anchor layout not anchoring?

I am trying to get the Label called "number" to show up directly over the center of the Ellipse. I have tried putting both in the same layout, in different layouts, in AnchorLayouts and RelativeLayouts, and I haven't been able to figure out how to to this.

Here's a test version of my python showing the same issue:

class BugTester(FloatLayout):
    def __init__(self):
        super().__init__()
        for i in range(1,6):
            temp  = GamePiece(5, "Red")
            temp.pos = (i*100,i*200)
            self.add_widget(temp)


class BugTestingApp(App):
    def build(self):
        return BugTester()

class GamePiece(ToggleButton):

    def __init__(self, number, color, **kwargs):
        self.number = number
        self.player_color = color
        self.background_normal = "5.png"
        super(GamePiece, self).__init__()


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

And my kivy:

<BugTester>:
    Button:
        background_disabled_normal: "background.jpg"
        disabled: True


<GamePiece>:
    id: gamepiece
    size_hint:(None, None)
    group: self.player_color
    border: (0,0,0,0)


    AnchorLayout:
        id: layout
        center: root.center
        on_size: self.size = root.size
        anchor_x: "left"
        anchor_y: "bottom"

        canvas:
            Color:
                rgba: 0,0,0,1
            Ellipse:
            #this is the ellipse in question
                id: circle
                size: (root.width/2.5, root.height/3)
                #pos: -15, -20
                pos: root.pos

        Label:
        #this is the label I want centered over the ellipse
            id: number
            text: str(root.number)
            color: (1,1,1,1)
            font_size: root.height/4
            bold: True
            #pos: root.pos

Here's what it currently looks like: (one of the togglebuttons is pressed for illustration purposes) Number not lining up

Upvotes: 2

Views: 1664

Answers (1)

jligeza
jligeza

Reputation: 4693

In your example, labels are as big as the anchor layouts they belong to, so you can't move them.

If you want them to have some other size, then disable size_hint, and use a fixed size (for instance, as big as the ellipses):

Label:
#this is the label I want centered over the ellipse
    size_hint: None, None
    size: (root.width/2.5, root.height/3)
    id: number
    text: str(root.number)
    color: (1,1,1,1)
    font_size: root.height/4
    bold: True
    #pos: root.pos

Result:

enter image description here

Upvotes: 2

Related Questions