Skip to main content
 首页 » 编程设计

python之我如何用 Python 在 Canvas 上手绘

2025年05月04日8lautakyan007

我希望能够用 Python 在 Canvas 上作画,并逐点记录笔画。这意味着用户在 Canvas 上单击,移动鼠标,再次抬起鼠标按钮,...

所以我想像在问题 How do I hand draw on canvas? 中那样使用 Python .

我如何使用 Python 执行此操作?

我不是在寻找一种方法来操纵图形/放置 SVG/...

请您参考如下方法:

您可以使用 Kivy 库,Kivy 网站上的启动教程之一是制作 simple painting app

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打个包,就是多了一点complicated