Mlody87
Mlody87

Reputation: 435

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: enter image description here

But I would like to get something like this: enter image description here

I would like to be able to draw only in this widget and provide positions of the drawn elements in relation to it.

Upvotes: 0

Views: 33

Answers (1)

John Anderson
John Anderson

Reputation: 39117

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

Upvotes: 1

Related Questions