如何用Python在画布上手绘?
我想用Python在一个画布上画画,并且记录下每一个笔画的具体点。这意味着用户在画布上点击一下,然后移动鼠标,再松开鼠标按钮,最后再点击一次,等等。
所以我想用Python做的事情,跟我在这个问题中做的类似:我怎么在画布上手绘?。
我该怎么用Python来实现这个呢?
我并不是想要处理图形或者放置SVG等其他东西。
1 个回答
2
你可以使用Kivy这个库,Kivy网站上有一个入门教程,教你如何制作一个简单的绘画应用
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse, Line
class MyPaintWidget(Widget):
def on_touch_down(self, touch):
with self.canvas:
Color(1, 1, 0)
d = 30.
Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
touch.ud['line'] = Line(points=(touch.x, touch.y))
def on_touch_move(self, touch):
touch.ud['line'].points += [touch.x, touch.y]
class MyPaintApp(App):
def build(self):
return MyPaintWidget()
if __name__ == '__main__':
MyPaintApp().run()
这个库在Windows和Unix系统上都能运行,包括OSX系统,经过Bulldozer打包后也可以在Android上使用(github)。你也可以为iOS制作一个包,但这个过程稍微有点复杂