-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy path06_canvas.py
More file actions
77 lines (65 loc) · 2.61 KB
/
06_canvas.py
File metadata and controls
77 lines (65 loc) · 2.61 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
#!/usr/bin/env python3
'''
Пример объектной организации кода
'''
from tkinter import *
from tkinter import colorchooser
class App(Frame):
'''Base framed application class'''
def __init__(self, master=None, Title="Application"):
Frame.__init__(self, master)
self.master.rowconfigure(0, weight=1)
self.master.columnconfigure(0, weight=1)
self.master.title(Title)
self.grid(sticky=N+E+S+W)
self.create()
self.adjust()
def create(self):
'''Create all the widgets'''
self.bQuit = Button(self, text='Quit', command=self.quit)
self.bQuit.grid()
def adjust(self):
'''Adjust grid sise/properties'''
# TODO Smart detecting resizeable/still cells
for i in range(self.size()[0]):
self.columnconfigure(i, weight=12)
for i in range(self.size()[1]):
self.rowconfigure(i, weight=12)
class Paint(Canvas):
'''Canvas with simple drawing'''
def mousedown(self, event):
'''Store mousedown coords'''
self.x0, self.y0 = event.x, event.y
self.cursor = None
def mousemove(self, event):
'''Do sometheing when drag a mouse'''
if self.cursor:
self.delete(self.cursor)
self.cursor = self.create_line((self.x0, self.y0, event.x, event.y), fill=self.foreground.get())
def mouseup(self, event):
'''Dragging is done'''
self.cursor = None
#print(self.find_all())
def __init__(self, master=None, *ap, foreground="black", **an):
self.foreground = StringVar()
self.foreground.set(foreground)
Canvas.__init__(self, master, *ap, **an)
self.bind("<Button-1>", self.mousedown)
self.bind("<B1-Motion>", self.mousemove)
self.bind("<ButtonRelease-1>", self.mouseup)
class MyApp(App):
def askcolor(self):
self.Canvas.foreground.set(colorchooser.askcolor()[1])
def create(self):
self.Canvas = Paint(self, foreground="midnightblue")
self.Canvas.grid(row=0, column=0, rowspan=3, sticky=N+E+S+W)
self.AskColor = Button(self, text="Color", command=self.askcolor)
self.AskColor.grid(row=0, column=1, sticky=N+W)
self.ShowColor = Label(self, textvariable=self.Canvas.foreground)
self.ShowColor.grid(row=1, column=1, sticky=N+W+E)
self.Quit = Button(self, text="Quit", command=self.quit)
self.Quit.grid(row=2, column=1, sticky=N+W)
app = MyApp(Title="Canvas Example")
app.mainloop()
for item in app.Canvas.find_all():
print(*app.Canvas.coords(item), app.Canvas.itemcget(item, "fill"))