简体   繁体   English

Kivy python 不画线

[英]Line is not drawn in Kivy python

Code:代码:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color
from kivy.graphics import Line


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

        # Widget has a property called canvas
        with self.canvas:
            Color(0, 1, 0, .5, mode='rgba')
            Line(points=(350, 400, 500, 800, 650, 400, 300, 650, 700, 650, 350, 400), width=3)

            Color(0, 0, 1, .5, mode='rgba')
            Line(bezier=(200, 100, 250, 150, 300, 50, 350, 100), width=3)


class MyApp(App):
    def build(self):
        return Draw()


if __name__ == "__main__":
    MyApp().run()


The result:结果:

Both the green line and the blue line should show up, but only the green line shows up The Green line shows a star The Blue line shows a wave绿线和蓝线都应该出现,但只有绿线出现 绿线显示星星 蓝线显示波浪

self.pos自我定位

If you resize the application from your code, you should notice that sometimes the blue line is plotted.如果您根据代码调整应用程序的大小,您应该注意到有时会绘制线。 This is because you did not bind the position of the widget with the canvas drawing (Line, Rectangle, etc...)这是因为您没有将小部件的position与 canvas 绘图(直线、矩形等...)绑定

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color
from kivy.graphics import Line


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

    def update_canvas(self, *args):
        # Widget has a property called canvas
        with self.canvas:
            # add self.pos to the line position
            Color(0, 1, 0, .5, mode='rgba')
            Line(pos=self.pos, points=(350, 400, 500, 800, 650, 400, 300, 650, 700, 650, 350, 400), width=3)

            Color(0, 0, 1, .5, mode='rgba')
            Line(pos=self.pos, bezier=(200, 100, 250, 150, 300, 50, 350, 100), width=3)


class MyApp(App):
    def build(self):
        return Draw()


if __name__ == "__main__":
    MyApp().run()

However, I noticed that sometimes the blue line is not visible depending on the OpenGL Context initialization.但是,我注意到有时蓝线不可见,具体取决于 OpenGL 上下文初始化。 The issue is, in general, caused by alpha channel of the color.通常,此问题是由颜色的Alpha通道引起的。

... if the current color has an alpha less than 1.0, a stencil will be used internally to draw the line. ...如果当前颜色的 alpha 小于 1.0,则会在内部使用模板来绘制线条。

This internal stencil has not a stable behavior.这个内部模板没有稳定的行为。

Source: Kivy.Lines来源: Kivy.Lines

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

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