2

I would like to be able to draw on a canvas with Python and record the strokes point by point. That means the user clicks on the canvas, moves the mouse, lifts the mouse button clicks again, ...

So I would like to work with Python similar like I did in the question How do I hand draw on canvas?.

How can I do this with Python?

I am NOT looking for a way to manipulate figures / place SVGs / ...

Community
  • 1
  • 1
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • I assume you are looking for a solution which does not happen inside a web browser? I sugget you take a look PyQt or PySide which are Python bindings for Qt native toolkit. – Mikko Ohtamaa Oct 21 '14 at 14:00
  • @MikkoOhtamaa I have never worked with Python and the web. So it could happen inside of a Web Browser. – Martin Thoma Oct 21 '14 at 18:08
  • In the web browser you can only run JavaScript. There are theoretical solutions which cross-compile Python to JavaScript, but those are academic exercises. Thus, you should pick one of the native application toolkits for Python. – Mikko Ohtamaa Oct 21 '14 at 18:32
  • Well, JavaScript could do the recording and send it via POST to a Python web server that only listens. That would eventually be the best solution. – Martin Thoma Oct 21 '14 at 19:24
  • Hmm you don't need real-time? But in the end, it will be much simpler just to learn JavaScript basics and and do it using browser `` element. In the order of several magnitudes. You may even find libraries and frameworks suitable for your purposes, with little modification needed. – Mikko Ohtamaa Oct 21 '14 at 20:17

1 Answers1

2

You could use the Kivy library, one of the startup tutorials on the Kivy website is making a 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()

This should work on Windows and Unix based systems including OSX, as well as Android after being packaged by Bulldozer (github). You can also make a package for iOS, but it's a little more complicated

user22723
  • 167
  • 1
  • 7
  • Can I use `argparse` with Kivy? When I use it like I am used to, then Kivy seems to "override" the argparse argument parsing. – Martin Thoma Oct 21 '14 at 18:02
  • I'm not sure how, but google lead me to a kivy program which uses it :http://pythonhosted.org/KiTT/_modules/kitt/kitt.html – user22723 Oct 21 '14 at 21:05