未在Kivy python中绘制线

hec6srdp  于 2023-01-29  发布在  Python
关注(0)|答案(1)|浏览(107)

代码:

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:
绿色线和蓝线都应显示,但只有绿线显示绿线显示星星蓝线显示波浪形

5ssjco0h

5ssjco0h1#

自身位置

如果你通过代码调整应用程序的大小,你应该注意到有时会出现蓝色线,这是因为你没有将小部件的 position 与画布绘图(Line、Rectangle等)绑定。

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

然而,我注意到有时候蓝线是不可见的,这取决于OpenGL上下文的初始化。这个问题通常是由颜色的alpha通道引起的。
...如果当前颜色的alpha小于1.0,则将在内部使用模具来绘制线条。
此内部模具的行为不稳定。
来源:Kivy.Lines

相关问题