• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1""""Paint program by Dave Michell.
2
3Subject: tkinter "paint" example
4From: Dave Mitchell <davem@magnet.com>
5To: python-list@cwi.nl
6Date: Fri, 23 Jan 1998 12:18:05 -0500 (EST)
7
8  Not too long ago (last week maybe?) someone posted a request
9for an example of a paint program using Tkinter. Try as I might
10I can't seem to find it in the archive, so i'll just post mine
11here and hope that the person who requested it sees this!
12
13  All this does is put up a canvas and draw a smooth black line
14whenever you have the mouse button down, but hopefully it will
15be enough to start with.. It would be easy enough to add some
16options like other shapes or colors...
17
18                                                yours,
19                                                dave mitchell
20                                                davem@magnet.com
21"""
22
23from Tkinter import *
24
25"""paint.py: not exactly a paint program.. just a smooth line drawing demo."""
26
27b1 = "up"
28xold, yold = None, None
29
30def main():
31    root = Tk()
32    drawing_area = Canvas(root)
33    drawing_area.pack()
34    drawing_area.bind("<Motion>", motion)
35    drawing_area.bind("<ButtonPress-1>", b1down)
36    drawing_area.bind("<ButtonRelease-1>", b1up)
37    root.mainloop()
38
39def b1down(event):
40    global b1
41    b1 = "down"           # you only want to draw when the button is down
42                          # because "Motion" events happen -all the time-
43
44def b1up(event):
45    global b1, xold, yold
46    b1 = "up"
47    xold = None           # reset the line when you let go of the button
48    yold = None
49
50def motion(event):
51    if b1 == "down":
52        global xold, yold
53        if xold is not None and yold is not None:
54            event.widget.create_line(xold,yold,event.x,event.y,smooth=TRUE)
55                          # here's where you draw it. smooth. neat.
56        xold = event.x
57        yold = event.y
58
59if __name__ == "__main__":
60    main()
61