Kivy-如何使画布高度小于父级高度 [英] Kivy-how can i make my canvas height smaller than the parent height

查看:87
本文介绍了Kivy-如何使画布高度小于父级高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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文件:

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?

推荐答案

Kivy画布不是小部件或绘画的空间.这只是一组说明.您无法调整大小.我想您想调整绘制矩形的大小.我不确定您期望得到什么结果,但是您可以使用父窗口小部件的widthheight属性来调整矩形的大小:

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

结果:

这篇关于Kivy-如何使画布高度小于父级高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆