blob: d46e20b9ade412c971e2d60205bae7fc2241b0cf [file] [log] [blame]
Guido van Rossum25f17221998-01-26 16:47:30 +00001""""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
Tim Peters182b5ac2004-07-18 06:16:08 +000015be enough to start with.. It would be easy enough to add some
Guido van Rossum25f17221998-01-26 16:47:30 +000016options like other shapes or colors...
17
Tim Peters182b5ac2004-07-18 06:16:08 +000018 yours,
19 dave mitchell
20 davem@magnet.com
Guido van Rossum25f17221998-01-26 16:47:30 +000021"""
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():
Tim Peters182b5ac2004-07-18 06:16:08 +000031 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()
Guido van Rossum25f17221998-01-26 16:47:30 +000038
39def b1down(event):
Tim Peters182b5ac2004-07-18 06:16:08 +000040 global b1
41 b1 = "down" # you only want to draw when the button is down
42 # because "Motion" events happen -all the time-
Guido van Rossum25f17221998-01-26 16:47:30 +000043
44def b1up(event):
Tim Peters182b5ac2004-07-18 06:16:08 +000045 global b1, xold, yold
46 b1 = "up"
47 xold = None # reset the line when you let go of the button
48 yold = None
Guido van Rossum25f17221998-01-26 16:47:30 +000049
50def motion(event):
Tim Peters182b5ac2004-07-18 06:16:08 +000051 if b1 == "down":
52 global xold, yold
53 if xold != None and yold != 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
Guido van Rossum25f17221998-01-26 16:47:30 +000058
59if __name__ == "__main__":
Tim Peters182b5ac2004-07-18 06:16:08 +000060 main()