Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BoxLayout background color in wrong position

Tags:

python

kivy

Given the layout below I expected the BoxLayout to be a 44px bar at the top of the screen with red background with two "Date time" labels. As shown in the screenshot, the labels are where they're expected (so the position of the box layout is correct), not so for the red background:

enter image description here.

I'm sure I'm missing something obvious as this is the very first time I use Kivy and KV. I tried using canvas.{before, after} to no avail. What am I missing? Thanks in advance for any answers!

#:kivy 1.0
#
FloatLayout:    
    canvas:
        Color:
            # #263238
            rgb: 0x26 / 255.0, 0x32 / 255.0, 0x38 / 255.0

        Rectangle:
            size: self.size

    BoxLayout:
        canvas:
            Color:
                rgb: 1, 0, 0
            Rectangle:
                size: self.size

        orientation: 'horizontal'
        padding: 10
        spacing: 10
        size_hint: 1, None
        pos_hint: {'top': 1}
        height: 44

        Label:
            height: 24
            text_size: self.width, None
            text: 'Date time'

        Label:
            height: 24
            text_size: self.width, None
            text: 'Date time 2'

    Label:
        text: 'testing'

Edit: as per Totem's answer, adding pos: self.pos to BoxLayout canvas rectangle does the trick.

like image 714
grendel Avatar asked Oct 16 '25 01:10

grendel


2 Answers

You're BoxLayout is where you intend it to be, as you mention. It's filled with two labels, which are also where they are meant to be. In order for the background of all this to look red, you could just make the labels backgrounds red. You've already made the BoxLayout red, it just can't be seen behind the labels, and I'm not entirely sure how to make those transparent(I'm sure it's possible). Learning kivy is really all about experimentation in the end, I think.

To make the labels have a red background(giving you the same effect), you probably could use their respective canvases, like you did with the BoxLayout, except using canvas.before. You wouldn't need to do it with either the float or box layouts then anymore.

From the comment: set the boxlayouts Rectangle position to pos: self.pos

like image 126
Totem Avatar answered Oct 17 '25 17:10

Totem


I fixed the problem. See the red area in the figure. BoxLayout's Background color output image. In this section i add pos:self.pos after self.size look like.

Rectangle:
    size: self.size
    pos:self.pos

It should work.

like image 31
MD. Saiful Islam Avatar answered Oct 17 '25 15:10

MD. Saiful Islam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!