-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path鼠标画折线.py
144 lines (123 loc) · 5.42 KB
/
鼠标画折线.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
from tkinter import *
class draw_polyline:
def __init__(self,canvas,vertical):
self.lines_list = []
self.canvas = canvas
self.vertical = vertical
self.click_num = 0
pass
def get_diagonal_coords(self):
self.x_list,self.y_list = [],[]
for i in self.lines_list:
coord = self.canvas.coords(i)
self.x_list.append(coord[0])
self.x_list.append(coord[2])
self.y_list.append(coord[1])
self.y_list.append(coord[3])
self.x_min = min(self.x_list)
self.y_min = min(self.y_list)
self.x_max = max(self.x_list)
self.y_max = max(self.y_list)
def start_draw_line(self,event):
x1,y1,x,y = event.x,event.y,event.x,event.y
if self.click_num == 0:
if self.vertical:
self.lines_list.append(self.canvas.create_line(x1,y1,x1,y))
else:
self.lines_list.append(self.canvas.create_line(x1,y1,x,y1))
else:
l = self.lines_list[self.click_num-1]
coords = self.canvas.coords(l)
if self.vertical:
self.lines_list.append(self.canvas.create_line(coords[2],coords[3],coords[2],y+5))
else:
self.lines_list.append(self.canvas.create_line(coords[2],coords[3],x+5,coords[3]))
canvas.bind("<B1-Motion>", self.move_and_draw)
canvas.bind("<ButtonRelease-1>", self.onrelease_handler)
def onrelease_handler(self,event):
self.vertical = not self.vertical
self.click_num+=1
def in_the_range(self,l,coord):
coords = self.canvas.coords(l)
x_min,x_max = min(coords[0],coords[2]),max(coords[0],coords[2])
y_min,y_max = min(coords[1],coords[3]),max(coords[1],coords[3])
x_in_range = coord[0]>x_min-3 and coord[0]<x_max+3
y_in_range = coord[1]>y_min-3 and coord[1]<y_max+3
return x_in_range and y_in_range
def start_draw_line_1(self,event):
self.start = (event.x,event.y)
x1,y1,x,y = event.x,event.y,event.x,event.y
if self.click_num == 0:
if self.vertical:
self.lines_list.append(self.canvas.create_line(x1,y1,x1,y+5,arrow = 'last',arrowshape=(8,15,3)))
else:
self.lines_list.append(self.canvas.create_line(x1,y1,x+5,y1,arrow = 'last',arrowshape=(8,15,3)))
canvas.bind("<B1-Motion>", self.move_and_draw_1)
# canvas.bind("<ButtonRelease-1>", self.onrelease_handler)
def move_and_draw_1(self,event):
l = self.lines_list[-1]
coords = self.canvas.coords(l)
x_in_range = event.x < canvas.winfo_width()-10 and event.x>10
y_in_range = event.y < canvas.winfo_height()-10 and event.y > 10
# print(l)
x0,y0,x,y = coords[0],coords[1],event.x,event.y
if event.x > canvas.winfo_width()-10:
x = canvas.winfo_width()-10
elif event.x<10:
x = 10
if event.y > canvas.winfo_height()-10:
y = canvas.winfo_height()-10
elif event.y < 10:
y = 10
if self.vertical:
if abs(y-y0)>20 or len(self.lines_list)==1:
self.canvas.coords(l,x0,y0,x0,y)
coords = self.canvas.coords(l)
if abs(x-x0)>20:
self.vertical = not self.vertical
self.canvas.itemconfig(l, arrow='')
self.lines_list.append(self.canvas.create_line(coords[2],coords[3],x,coords[3],arrow = 'last',arrowshape=(8,15,3)))
elif abs(y-y0)<20:
self.canvas.delete(l)
self.lines_list.pop(-1)
self.canvas.itemconfig(self.lines_list[-1], arrow = 'last',arrowshape=(8,15,3))
# print('delete')
self.vertical = not self.vertical
elif not self.vertical:
if abs(x-x0)>20 or len(self.lines_list)==1:
self.canvas.coords(l,x0,y0,x,y0)
coords = self.canvas.coords(l)
if abs(y-y0)>20:
self.vertical = not self.vertical
self.canvas.itemconfig(l, arrow='')
self.lines_list.append(self.canvas.create_line(coords[2],coords[3],coords[2],y,arrow = 'last',arrowshape=(8,15,3)))
elif abs(x-x0)<20:
self.canvas.delete(l)
self.lines_list.pop(-1)
self.canvas.itemconfig(self.lines_list[-1], arrow = 'last',arrowshape=(8,15,3))
# print('delete')
self.vertical = not self.vertical
def move_and_draw(self,event):
if len(self.lines_list)>0:
l = self.lines_list[self.click_num]
coords = self.canvas.coords(l)
x1,y1,x,y = coords[0],coords[1],event.x,event.y
if self.vertical:
if y-y1<5:
y = int(y+5*abs(y-y1)/((y-y1)+1e-9))
self.canvas.coords(l,x1,y1,x1,y)
else:
if x-x1<5:
x = int(x+5*abs(x-x1)/((x-x1)+1e-9))
self.canvas.coords(l,x1,y1,x,y1)
def onclick_handler(self,event):
self.canvas.bind("<Button-1>", self.start_draw_line)
def start_draw(self):
self.canvas.bind("<Button-1>", self.start_draw_line_1)
master = Tk()
master.title("鼠标画折线")
canvas = Canvas(master, width=500, height=500)
canvas.pack()
p = draw_polyline(canvas = canvas,vertical=True)
p.start_draw()
master.mainloop()