簡體   English   中英

為小部件指定大小並在其中繪圖

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

我想創建一個具有固定大小和背景的小部件。 它首先要添加到 BoxLayout 中。 我想在此小部件內畫一條線,以便它僅在其中可見並與它相關聯。 通過輸入 (0,0) 行的位置,我的意思是小部件的開頭而不是整個應用程序窗口。 如何達到這種效果?

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

和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"

現在我有這樣的事情: 在此處輸入圖片說明

但我想得到這樣的東西: 在此處輸入圖片說明

我希望能夠只在這個小部件中繪制並提供與其相關的繪制元素的位置。

您只需要調整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