-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpypaint.py
145 lines (99 loc) · 4.65 KB
/
pypaint.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from tkinter import *
import tkinter.font
class PaintApp:
drawing_tool = "rectangle"
left_but = "up"
x_pos, y_pos = None, None
x1_line_pt, y1_line_pt, x2_line_pt, y2_line_pt = None, None, None, None
def left_but_down(self, event=None):
self.left_but = "down"
self.x1_line_pt = event.x
self.y1_line_pt = event.y
def left_but_up(self, event=None):
self.left_but = "up"
# Reset the line
self.x_pos = None
self.y_pos = None
# Set x & y when mouse is released
self.x2_line_pt = event.x
self.y2_line_pt = event.y
# If mouse is released and line tool is selected
# draw the line
if self.drawing_tool == "line":
self.line_draw(event)
elif self.drawing_tool == "arc":
self.arc_draw(event)
elif self.drawing_tool == "oval":
self.oval_draw(event)
elif self.drawing_tool == "rectangle":
self.rectangle_draw(event)
elif self.drawing_tool == "text":
self.text_draw(event)
# ---------- CATCH MOUSE MOVEMENT ----------
def motion(self, event=None):
if self.drawing_tool == "pencil":
self.pencil_draw(event)
# ---------- DRAW PENCIL ----------
def pencil_draw(self, event=None):
if self.left_but == "down":
# Make sure x and y have a value
if self.x_pos is not None and self.y_pos is not None:
event.widget.create_line(self.x_pos, self.y_pos, event.x, event.y, smooth=TRUE)
self.x_pos = event.x
self.y_pos = event.y
# ---------- DRAW LINE ----------
def line_draw(self, event=None):
# Shortcut way to check if none of these values contain None
if None not in (self.x1_line_pt, self.y1_line_pt, self.x2_line_pt, self.y2_line_pt):
event.widget.create_line(self.x1_line_pt, self.y1_line_pt, self.x2_line_pt, self.y2_line_pt, smooth=TRUE, fill="green")
# ---------- DRAW ARC ----------
def arc_draw(self, event=None):
# Shortcut way to check if none of these values contain None
if None not in (self.x1_line_pt, self.y1_line_pt, self.x2_line_pt, self.y2_line_pt):
coords = self.x1_line_pt, self.y1_line_pt, self.x2_line_pt, self.y2_line_pt
# start : starting angle for the slice in degrees
# extent : width of the slice in degrees
# fill : fill color if needed
# style : can be ARC, PIESLICE, or CHORD
event.widget.create_arc(coords, start=0, extent=150,
style=ARC)
# ---------- DRAW OVAL ----------
def oval_draw(self, event=None):
if None not in (self.x1_line_pt, self.y1_line_pt, self.x2_line_pt, self.y2_line_pt):
# fill : Color option names are here http://wiki.tcl.tk/37701
# outline : border color
# width : width of border in pixels
event.widget.create_oval(self.x1_line_pt, self.y1_line_pt, self.x2_line_pt, self.y2_line_pt,
fill="midnight blue",
outline="yellow",
width=2)
# ---------- DRAW RECTANGLE ----------
def rectangle_draw(self, event=None):
if None not in (self.x1_line_pt, self.y1_line_pt, self.x2_line_pt, self.y2_line_pt):
# fill : Color option names are here http://wiki.tcl.tk/37701
# outline : border color
# width : width of border in pixels
event.widget.create_rectangle(self.x1_line_pt, self.y1_line_pt, self.x2_line_pt, self.y2_line_pt,
fill="midnight blue",
outline="yellow",
width=2)
# ---------- DRAW TEXT ----------
def text_draw(self, event=None):
if None not in (self.x1_line_pt, self.y1_line_pt):
# Show all fonts available
print(tkinter.font.families())
text_font = tkinter.font.Font(family='Helvetica',
size=20, weight='bold', slant='italic')
event.widget.create_text(self.x1_line_pt, self.y1_line_pt,
fill="green",
font=text_font,
text="WOW")
def __init__(self, root):
drawing_area = Canvas(root)
drawing_area.pack()
drawing_area.bind("<Motion>", self.motion)
drawing_area.bind("<ButtonPress-1>", self.left_but_down)
drawing_area.bind("<ButtonRelease-1>", self.left_but_up)
root = Tk()
paint_app = PaintApp(root)
root.mainloop()