-
Notifications
You must be signed in to change notification settings - Fork 0
/
tk_canvas.py
57 lines (40 loc) · 1.52 KB
/
tk_canvas.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
from tkinter import *
from tkinter import ttk
from tkcolorpicker import askcolor
class DrawingApp:
def __init__(self, parent):
self.parent = parent
self.canvas = Canvas(self.parent, width = 640, height = 480, background = 'white')
self.canvas.pack()
self.canvas.bind('<B1-Motion>', self.draw)
self.button = ttk.Button(self.parent, text='Pick color', command=self.pick_color)
self.button.pack()
self.parent.option_add('*tearOff', False)
self.menubar = Menu(self.parent)
self.parent.config(menu = self.menubar)
self.file = Menu(self.menubar)
self.menubar.add_cascade(menu=self.file, label='File')
self.file.add_command(label='New', command = main)
self.file.entryconfig('New', accelerator = 'Ctrl+N')
def initial_coordinates(self, event):
global prev
self.prev = event
def pick_color(self):
global color
color = askcolor()[1]
print(color)
def draw(self, event):
self.event = event
try:
self.canvas.create_line(self.prev.x, self.prev.y, self.event.x, self.event.y, fill=color, width = 5)
except AttributeError:
pass
self.prev = self.event
def main():
global color
color = 'black'
root = Tk()
DrawingApp(root)
root.mainloop
if __name__ == "__main__":
main()