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