Python for Kids

Puzzle Number 7

Posted, 24 Apr 2013

Difficulty: medium

In Chapter 12, you learn how to draw simple shapes using the tkinter canvas, and how to bind keys to functions and perform basic animations. You can bind the mouse to functions in the same way. For example, the event '<Motion>' is fired every time the mouse moves, '<ButtonPress-1>' is fired when the left mouse button is pressed, and '<ButtonRelease-1>' is fired when the button is released.

With those events, and using the canvas, you can create a simple drawing program. When you click on the canvas and drag the mouse your program will draw a line. Release the button and it stops drawing.

Hint: you might need to use the mainloop function of Tk to get this program to work properly.


Update: The full solution can be found here.