Robert Benassai
Robert Benassai

Reputation: 51

How to put a Lable on a Rectangle in Kivy

I am quite a novice programming with kivy, and I just wanted to put a green rectangle under a label to make it look a little bit like a display. I've tried making a label and then, inside of it, a canvas with the rectangle, but when it's time to put them together, the self.pos doesn't put them on the same place... Here's part of the code:

'''.kv file'''
Label:
            id: s_label
            size:50,50
            text:'[font=Digital-7][color=D20000][size=24] S= [/color][/font][/size]'
            markup:True
            pos_hint:{'x':0.3,'y':-0.2}
            canvas:
            canvas.before:
                Color:
                    rgba: 0, 153, 0, 0.5
                RoundedRectangle:
                    pos: self.pos[0] , self.pos[1]
                    size: (self.width/6, self.width/16)
                    id: S_rect

The Label is inside a FloatLayout, but I don't know if that is relevant. this is what I get. See, the green rectangle is far away from the red S label

Upvotes: 1

Views: 49

Answers (1)

John Anderson
John Anderson

Reputation: 38937

Two problems with your code:

  1. You must include size_hint: None, None in your Label in the kv. Without that, the size has no effect, and the Label fills its parent.
  2. The pos_hint:{'x':0.3,'y':-0.2} positions the Label below the bottom of the parent. Try something like pos_hint:{'x':0.3,'y':0.2}.

Upvotes: 1

Related Questions