简体   繁体   中英

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. 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. 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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