简体   繁体   English

为小部件指定大小并在其中绘图

[英]Giving sizes to the widget and drawing in it

I would like to create a widget with a fixed size and background.我想创建一个具有固定大小和背景的小部件。 It is to be added first in BoxLayout.它首先要添加到 BoxLayout 中。 I would like to draw a line inside this widget so that it is visible only in it and placed in relation to it.我想在此小部件内画一条线,以便它仅在其中可见并与它相关联。 By entering (0,0) the position of the line I mean the beginning of the widget and not the entire application window.通过输入 (0,0) 行的位置,我的意思是小部件的开头而不是整个应用程序窗口。 How to achieve this effect?如何达到这种效果?

from random import random
from kivy.app import App
from kivy.graphics import Color, Ellipse, Line
from kivy.uix.button import Button
from kivy.uix.widget import Widget

class CombWidget(Widget):
    pass

class MyPaintWidget(Widget):
    def __init__(self, **kwargs):
        super(MyPaintWidget, self).__init__(**kwargs)

class MyPaintApp(App):
    def build(self):
        return CombWidget()

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

and kv file和kv文件

<CombWidget>:
    BoxLayout:
        orientation: 'vertical'
        size: root.size
        padding: 20
        spacing: 50

        MyPaintWidget:
            size: 400, 400
            size_hint: 400, 400

            canvas.before:
                Color:
                    rgba: 1, 1, 1, 1
                Rectangle:
                    pos: self.pos
                    size: self.size

            canvas:
                Color:
                    rgba: 0, 0, 0, 1
                Line:
                    points: 0, 0, 200, 200


        Button:
            text: "Hallo"
        Button:
            text: "Hallo 1"
        Button:
            text: "Hallo 2"

Right now I have something like this:现在我有这样的事情: 在此处输入图片说明

But I would like to get something like this:但我想得到这样的东西: 在此处输入图片说明

I would like to be able to draw only in this widget and provide positions of the drawn elements in relation to it.我希望能够只在这个小部件中绘制并提供与其相关的绘制元素的位置。

You just need to adjust the points of the Line :您只需要调整Linepoints

        canvas:
            Color:
                rgba: 0, 0, 0, 1
            Line:
                points: self.x, self.y, self.x + 200, self.y + 200

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM