home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Demo / tkinter / guido / paint.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  1.6 KB  |  61 lines

  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
  15. be enough to start with.. It would be easy enough to add some 
  16. options like other shapes or colors...
  17.  
  18.                         yours,
  19.                         dave mitchell
  20.                         davem@magnet.com
  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():
  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.  
  39. def 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.  
  44. def 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.  
  50. def motion(event):
  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
  58.  
  59. if __name__ == "__main__":
  60.   main()
  61.