Guido van Rossum | 25f1722 | 1998-01-26 16:47:30 +0000 | [diff] [blame] | 1 | """"Paint program by Dave Michell. |
| 2 | |
| 3 | Subject: tkinter "paint" example |
| 4 | From: Dave Mitchell <davem@magnet.com> |
| 5 | To: python-list@cwi.nl |
| 6 | Date: Fri, 23 Jan 1998 12:18:05 -0500 (EST) |
| 7 | |
| 8 | Not too long ago (last week maybe?) someone posted a request |
| 9 | for an example of a paint program using Tkinter. Try as I might |
| 10 | I can't seem to find it in the archive, so i'll just post mine |
| 11 | here 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 |
| 14 | whenever you have the mouse button down, but hopefully it will |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 15 | be enough to start with.. It would be easy enough to add some |
Guido van Rossum | 25f1722 | 1998-01-26 16:47:30 +0000 | [diff] [blame] | 16 | options like other shapes or colors... |
| 17 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 18 | yours, |
| 19 | dave mitchell |
| 20 | davem@magnet.com |
Guido van Rossum | 25f1722 | 1998-01-26 16:47:30 +0000 | [diff] [blame] | 21 | """ |
| 22 | |
| 23 | from Tkinter import * |
| 24 | |
| 25 | """paint.py: not exactly a paint program.. just a smooth line drawing demo.""" |
| 26 | |
| 27 | b1 = "up" |
| 28 | xold, yold = None, None |
| 29 | |
| 30 | def main(): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 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() |
Guido van Rossum | 25f1722 | 1998-01-26 16:47:30 +0000 | [diff] [blame] | 38 | |
| 39 | def b1down(event): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 40 | 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 Rossum | 25f1722 | 1998-01-26 16:47:30 +0000 | [diff] [blame] | 43 | |
| 44 | def b1up(event): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 45 | 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 Rossum | 25f1722 | 1998-01-26 16:47:30 +0000 | [diff] [blame] | 49 | |
| 50 | def motion(event): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 51 | 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 Rossum | 25f1722 | 1998-01-26 16:47:30 +0000 | [diff] [blame] | 58 | |
| 59 | if __name__ == "__main__": |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 60 | main() |