<DrawCanvasWidget>:
canvas.before:
Color:
rgba:[1, 1, 1, 1]
Rectangle:
pos: self.pos
size: self.size
from kivy.app import App
from kivy.graphics import Line, Color
from kivy.uix.widget import Widget
class DrawCanvasWidget(Widget):#DrawCanvasWidget类监听鼠标事件
def __init__(self, **kwargs):
super().__init__(**kwargs)
# 设置默认颜色
self.canvas.add(Color(rgb=[0,0,0]))
self.line_width = 2 #设置线宽
def on_touch_down(self, touch):#kivy提供函数,鼠标点击
"""触摸显示轨迹"""
if Widget.on_touch_down(self, touch):
return
with self.canvas:
touch.ud['current_line'] = Line(points=(touch.x, touch.y), width=self.line_width)
def on_touch_move(self, touch):#kivy提供函数
"""连线"""
if 'current_line' in touch.ud:
touch.ud['current_line'].points += (touch.x, touch.y)
class PaintApp(App):
def build(self):
self.draw_canvas_widget = DrawCanvasWidget()
return self.draw_canvas_widget
if __name__ == '__main__':
PaintApp().run()
kivy学习完全解析 012 画图板
最新推荐文章于 2022-10-31 11:17:15 发布