-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaint.py
More file actions
96 lines (62 loc) · 1.98 KB
/
paint.py
File metadata and controls
96 lines (62 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import tkinter
import tkinter.colorchooser
MASTER = tkinter.Tk()
MASTER.title("Paint")
CANVAS = tkinter.Canvas(MASTER, bg="white", width=500, height=500)
objects = []
last = None
tool = CANVAS.create_line
color = "black"
start_x = 0
start_y = 0
def lock(event: tkinter.Event) -> None:
global start_x, start_y
start_x = event.x
start_y = event.y
def move(event: tkinter.Event) -> None:
global last
CANVAS.delete(last)
last = tool(start_x, start_y, event.x, event.y, fill=color)
def release(event: tkinter.Event) -> None:
CANVAS.delete(last)
objects.append(tool(start_x, start_y, event.x, event.y, fill=color))
def undo() -> None:
if objects:
CANVAS.delete(objects.pop())
def clear() -> None:
CANVAS.delete("all")
objects = []
def choose_color() -> None:
global color
temp = tkinter.colorchooser.askcolor()[1]
if temp:
color = temp
def line() -> None:
global tool
tool = CANVAS.create_line
def rect() -> None:
global tool
tool = CANVAS.create_rectangle
def oval() -> None:
global tool
tool = CANVAS.create_oval
UNDO = tkinter.Button(MASTER, text="undo", command=undo)
CLEAR = tkinter.Button(MASTER, text="clear", command=clear)
COLOR = tkinter.Button(MASTER, text="color", command=choose_color)
LINE = tkinter.Button(MASTER, text="line", command=line)
RECT = tkinter.Button(MASTER, text="rectangle", command=rect)
OVAL = tkinter.Button(MASTER, text="oval", command=oval)
def main() -> None:
CANVAS.bind("<Button-1>", lock)
CANVAS.bind("<B1-Motion>", move)
CANVAS.bind("<ButtonRelease-1>", release)
CANVAS.grid(row=0, column=0, columnspan=8)
UNDO.grid(row=1, column=0)
CLEAR.grid(row=1, column=1)
COLOR.grid(row=1, column=2)
LINE.grid(row=1, column=5)
RECT.grid(row=1, column=6)
OVAL.grid(row=1, column=7)
MASTER.mainloop()
if __name__ == '__main__':
main()