5

I have a stacklayout with a canvas and an image. my image has a size_hint of .1 and I want my canvas to have the same height as my image.

.kv file:

StackLayout:
    orientation: 'lr-tb'
    canvas:
        Color:
            rgba: 1,1,1,1
        Rectangle:
            pos: self.pos
            size: self.size
    Image:              
        size_hint_y: .1
        source: 'Images\login\cptbanner.jpg'
        allow_stretch: True
        keep_ratio: True

what can I do to get the desired effect?

supreme
  • 353
  • 3
  • 14

1 Answers1

5

A Kivy canvas is not a widget or the space in which you paint. It is only a set of instructions. You can´t resize it. I guess you want to resize the drawn rectangle. I'm not sure what the outcome is you expect, but you can resize the rectangle using the width and height attributes of the parent widget:

from kivy.app import App
from kivy.base import Builder
from kivy.uix.boxlayout import BoxLayout


Builder.load_string("""
<MainWindow>:
    StackLayout:
        id : aa
        orientation: 'lr-tb'

        canvas:
            Color:
                rgba: 1,1,1,1
            Rectangle:
                pos: 0, self.height*0.9
                size: self.width, self.height*0.1

        Image:
            id: im
            size_hint_y: 0.1
            source: 'Images\login\cptbanner.jpg'
            allow_stretch: True
            keep_ratio: True
""")

class MainWindow(BoxLayout):
    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)

class MyApp(App):
    def build(self):
        return MainWindow()

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

Result:

enter image description here

FJSevilla
  • 3,733
  • 1
  • 13
  • 20
  • this is exactly what I needed. can you please explain why your multiplying self.height*0.9 and self.height*0.1? thanks! – supreme Jun 20 '17 at 22:02
  • 1
    `size: self.width, self.height*0.1` is equivalent to `size_hint: 1, 0.1` (we can´t use `size_hint` with Rectangle). `Pos: 0, self.height * 0.9` allows us to position the rectangle in the correct way --> x = 0, y = (height of the father - height of the rectangle). (height of the father - height of the rectangle) is the same as (height of the father * 0.9). I hope you get something out of this, my English is not very good ... :( – FJSevilla Jun 20 '17 at 22:28
  • your English is good enough brother, I understood everything perfectly, thank you for all the help. – supreme Jun 20 '17 at 22:33