Kivy Python线条绘制异常求助:仅绿色线条显示,蓝色贝塞尔线缺失
问题解决:Kivy中贝塞尔线条未显示
你的代码逻辑本身没有错误,蓝色贝塞尔线条未显示的原因是它的坐标位于窗口底部区域,可能因窗口默认高度不足或被系统界面遮挡导致不可见,或者位置过低不易察觉。
以下是两种可行的解决方案:
方案一:调整贝塞尔曲线的位置
将贝塞尔曲线的y坐标抬高,使其处于窗口的中间可见区域,修改后的代码如下:
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) 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) # 抬高贝塞尔曲线的y坐标,使其位于窗口中间 Color(0, 0, 1, 1, mode='rgba') # 同时将透明度设为1,更易观察 Line(bezier=(200, 400, 250, 450, 300, 350, 350, 400), width=3) class MyApp(App): def build(self): return Draw() if __name__ == "__main__": MyApp().run()
方案二:强制设置窗口大小
通过设置窗口尺寸,确保所有线条都在可见范围内,代码如下:
from kivy.app import App from kivy.uix.widget import Widget from kivy.graphics import Color, Line from kivy.core.window import Window # 导入窗口模块 class Draw(Widget): def __init__(self, **kwargs): super(Draw, self).__init__(**kwargs) 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() def on_start(self): Window.size = (800, 800) # 设置窗口宽高为800x800 if __name__ == "__main__": MyApp().run()
内容的提问来源于stack exchange,提问作者Martin
相关产品推荐
相关产品推荐

