-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpaint-pogram-basic.py
70 lines (56 loc) · 2.42 KB
/
paint-pogram-basic.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
from tkinter import *
from tkinter import ttk, colorchooser
class main:
def __init__(self, master):
self.master = master
self.color_fg = 'black'
self.color_bg = 'white'
self.old_x = None
self.old_y = None
self.penwidth = 5
self.drawWidgets()
self.c.bind('<B1-Motion>', self.paint) # drwaing the line
self.c.bind('<ButtonRelease-1>', self.reset)
def paint(self, e):
if self.old_x and self.old_y:
self.c.create_line(self.old_x, self.old_y, e.x, e.y, width=self.penwidth, fill=self.color_fg,
capstyle=ROUND, smooth=True)
self.old_x = e.x
self.old_y = e.y
def reset(self, e): # reseting or cleaning the canvas
self.old_x = None
self.old_y = None
def changeW(self, e): # change Width of pen through slider
self.penwidth = e
def clear(self):
self.c.delete(ALL)
def change_fg(self): # changing the pen color
self.color_fg = colorchooser.askcolor(color=self.color_fg)[1]
def change_bg(self): # changing the background color canvas
self.color_bg = colorchooser.askcolor(color=self.color_bg)[1]
self.c['bg'] = self.color_bg
def drawWidgets(self):
self.controls = Frame(self.master, padx=5, pady=5)
Label(self.controls, text='Pen Width:', font=('arial 18')).grid(row=0, column=0)
self.slider = ttk.Scale(self.controls, from_=5, to=100, command=self.changeW, orient=VERTICAL)
self.slider.set(self.penwidth)
self.slider.grid(row=0, column=1, ipadx=30)
self.controls.pack(side=LEFT)
self.c = Canvas(self.master, width=500, height=400, bg=self.color_bg, )
self.c.pack(fill=BOTH, expand=True)
menu = Menu(self.master)
self.master.config(menu=menu)
filemenu = Menu(menu)
colormenu = Menu(menu)
menu.add_cascade(label='Colors', menu=colormenu)
colormenu.add_command(label='Brush Color', command=self.change_fg)
colormenu.add_command(label='Background Color', command=self.change_bg)
optionmenu = Menu(menu)
menu.add_cascade(label='Options', menu=optionmenu)
optionmenu.add_command(label='Clear Canvas', command=self.clear)
optionmenu.add_command(label='Exit', command=self.master.destroy)
if __name__ == '__main__':
root = Tk()
main(root)
root.title('Application')
root.mainloop()