-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
287 lines (235 loc) · 8.17 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from tkinter import *
from tkinter import colorchooser
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
from io import BytesIO
from os import path
from PIL import Image, ImageTk, UnidentifiedImageError
brush_color = "black"
brush_size = 10
current_image_path = None
old_image_path = None
def set_title():
if current_image_path:
root.title(f"{path.split(current_image_path)[1]} - BAINT")
else:
root.title("untitled - BAINT")
def get_current_image_path():
if current_image_path == None:
set_current_image_path(filedialog.asksaveasfilename())
return current_image_path
def set_current_image_path(new):
global current_image_path, old_image_path
old_image_path = current_image_path
current_image_path = new
set_title()
def restore_image_path():
global current_image_path
current_image_path = old_image_path
set_title()
def open_image():
file_path = filedialog.askopenfilename()
try:
image = Image.open(file_path)
except UnidentifiedImageError:
messagebox.showwarning(title="warning", message="wrong file")
return
photo = ImageTk.PhotoImage(image)
canvas.config(width=photo.width(), height=photo.height())
canvas.create_image(0, 0, anchor=NW, image=photo)
canvas.image = photo
set_current_image_path(file_path)
def save():
print(current_image_path, old_image_path)
ps = canvas.postscript(colormode='color')
image = Image.open(BytesIO(ps.encode('utf-8')))
try:
image.save(get_current_image_path())
except:
restore_image_path()
messagebox.showwarning(title="warning", message="wrong path")
def save_as():
set_current_image_path(filedialog.asksaveasfilename())
save()
def Choose_color():
#asking user to choose color using deafult color choosing dialog window
global brush_color
brush_color = colorchooser.askcolor()[1]#getting color value, "[1]" gets only hex skipping rgb value
def New_image():
canvas.delete("all")
WSize = Toplevel()
WSize.title("Set painting space size")
WSize.transient(root)
WSize.grab_set()
HLabel = ttk.Label(WSize, text="Height")
HLabel.grid(column=0,row=0,padx=10,pady=10)
WLabel = ttk.Label(WSize, text="Width")
WLabel.grid(column=0,row=1,padx=10,pady=10)
MinHeight=1
current_height= DoubleVar(value=MinHeight)
height = ttk.Spinbox(
WSize,
from_=1,
to_=1000000,
textvariable=current_height,
)
height.grid(column=1, row=0, padx=10, pady=10)
MinWidth=1
current_width= DoubleVar(value=MinWidth)
width = ttk.Spinbox(
WSize,
from_=1,
to_=1000000,
textvariable=current_width,
)
width.grid(column=1, row=1, padx=10, pady=10)
def NewWindowSize():
try:
int_width = int(width.get())
int_height = int(height.get())
except ValueError:
messagebox.showwarning(title="warning", message="wrong size")
return
canvas.config(width=int_width, height=int_height)
WSize.destroy()
ok_button = ttk.Button(
WSize,
text = "Ok",
command=NewWindowSize)
ok_button.grid(column=2, row=2, padx=10, pady=10)
class BrushSizeDialog(Toplevel):
def __init__(self):
# create window and set properties
super().__init__(root)
self.title("Brush size")
self.rowconfigure(0, weight=2)
self.transient(root)
self.grab_set()
self.bind("<Escape>", self.close)
self.current_value = DoubleVar(value=brush_size)
self.max_value = 200
self.create_widgets()
def create_widgets(self):
#create frames and separator
top_frame = ttk.Frame(self)
separator = ttk.Separator(self)
bottom_frame = ttk.Frame(self)
top_frame.grid(row=0, padx=10, pady=10, sticky="N")
separator.grid(row=1, sticky="EW")
bottom_frame.grid(row=2, sticky="S")
#create widgets for top_frame
self.slider = ttk.Scale(
top_frame,
from_ = 1,
to = self.max_value,
orient = "horizontal",
length = 250,
variable = self.current_value,
command = self.set_slider_to_int
)
self.spinbox = ttk.Spinbox(
top_frame,
from_=1,
to=self.max_value,
textvariable=self.current_value,
command=self.set_spinbox_to_int
)
self.slider.grid(column=0, row=0, padx=10, pady=10)
self.spinbox.grid(column=1, row=0, padx=10, pady=10)
#create widgets for bottom_frame
cancel_button = ttk.Button(
bottom_frame,
text = "Cancel",
command = self.close
)
ok_button = ttk.Button(
bottom_frame,
text = "Ok",
command=self.accept_new_size
)
cancel_button.grid(column=0, row=0, padx=10, pady=10)
ok_button.grid(column=1, row=0, padx=10, pady=10)
def close(self, event=None):#closing window
self.destroy()
def set_slider_to_int(self, num):#user can change brush size using slider
if float(num).is_integer():#checking is 'num' an int to avoid errors
pass
else:
new = int(float(num))
self.slider.set(new)
def set_spinbox_to_int(self):
current = self.spinbox.get()
current = float(current)
if current.is_integer():#checking is 'current' an int to avoid errors
pass
else:
new = int(current)
self.spinbox.set(new)
def accept_new_size(self):
global brush_size
try:
brush_size = int(self.current_value.get())
except:
messagebox.showwarning(title="warning", message="size must be a number")
return
self.close()
previous = (0,0)#creating variable "previous" to check last mouse location
def draw_circle(center):
x = center[0]
y = center[1]
radius = brush_size / 2
top_left = (x - radius, y - radius)
bottom_right = (x + radius - 1, y + radius - 1)
canvas.create_oval(*top_left, *bottom_right, outline=brush_color, fill=brush_color)
def select(event): #funcion to draw dots
global previous
#widget = event.widget
draw_circle((event.x, event.y))
previous = (event.x, event.y)#changng previous so it will be accurate next time
def drag(event): #function to draw lines when dragging mouse
global previous
canvas.create_line(previous[0], previous[1], event.x, event.y, fill=brush_color, width=brush_size)
draw_circle((event.x, event.y))
#!Important, this feature is currently bugged, it isn't drawing how it is supposed to look like
#possible fix(isn't tested) can be changing it from line to oval, this may make this function unnecesarry
widget = event.widget
previous = (event.x, event.y)#changng previous so it will be accurate next time
root = Tk()
root.minsize(400,500) # setting minimal windows size STC
set_title()
application = ttk.Frame(root, padding=10) # creating frame for root window
application.grid()#adding grid to frame created 2 lines ago
# Menubar setup
menubar = Menu(root)
root.config(menu=menubar)
menu_tree = {
"file": [
["new", New_image],
["open", open_image],
["save_as", save_as],
["save", save],
["exit", root.destroy],
],
"brush": [
["color", Choose_color],
["size", BrushSizeDialog],
],
}
for menu_name in menu_tree:
new_menu = Menu(menubar)
for item in menu_tree[menu_name]:
new_menu.add_command(
label=item[0],
command=item[1]
)
menubar.add_cascade(
label=menu_name,
menu=new_menu
)
canvas = Canvas(root, cursor="circle", width=300, height=300, bg="#ffffff")#creating canva to draw on, setting circle cursor to make drawing experience better
canvas.grid()#creating grid for canvas
# Bind mouse events to methods (could also be in the constructor)
canvas.bind("<Button-1>", select)
canvas.bind("<B1-Motion>", drag)
root.mainloop()