Kivy-如何让我的canvas身高小于父身高

Kivy-how can i make my canvas height smaller than the parent height

我有一个带有 canvas 和图像的堆栈布局。我的图片的 size_hint 为 .1,我希望我的 canvas 与我的图片具有相同的高度。

.kv 文件:

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

我该怎么做才能达到预期的效果?

Kivy canvas 不是小部件,也不是您在其中绘画的 space。它只是一组指令。你不能调整它的大小。我猜您想调整绘制的矩形的大小。我不确定您期望的结果是什么,但您可以使用父小部件的 widthheight 属性调整矩形的大小:

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()

结果: