-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtkinterDnD.py
More file actions
46 lines (46 loc) · 1.75 KB
/
tkinterDnD.py
File metadata and controls
46 lines (46 loc) · 1.75 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
global grid
grid = 16
def make_draggable(widget):
widget.bind("<Button-1>", on_drag_start)
widget.bind("<B1-Motion>", on_drag_motion)
widget.bind("<ButtonRelease-1>", on_drag_release)
def make_draggable_component(widget):
widget.bind("<Button-1>", on_component_drag_start)
widget.bind("<B1-Motion>", on_component_drag_motion)
widget.bind("<ButtonRelease-1>", on_component_drag_release)
def on_drag_start(event):
widget = event.widget
widget._drag_start_x = event.x
widget._drag_start_y = event.y
def on_drag_motion(event):
widget = event.widget
x = widget.winfo_x() - widget._drag_start_x + event.x
y = widget.winfo_y() - widget._drag_start_y + event.y
widget.place(x=x, y=y)
def on_drag_release(event):
global grid
widget = event.widget
x = round((widget.winfo_x() - widget._drag_start_x + event.x) / grid) * grid
y = round((widget.winfo_y() - widget._drag_start_y + event.y) / grid) * grid
widget.place(x=x, y=y)
def on_component_drag_start(event):
widget = event.widget
container = widget.nametowidget(widget.winfo_parent())
container._drag_start_x = event.x
container._drag_start_y = event.y
def on_component_drag_motion(event):
widget = event.widget
container = widget.nametowidget(widget.winfo_parent())
x = container.winfo_x() - container._drag_start_x + event.x
y = container.winfo_y() - container._drag_start_y + event.y
container.place(x=x, y=y)
def on_component_drag_release(event):
global grid
widget = event.widget
container = widget.nametowidget(widget.winfo_parent())
x = round((container.winfo_x() - container._drag_start_x + event.x) / grid) * grid
y = round((container.winfo_y() - container._drag_start_y + event.y) / grid) * grid
container.place(x=x, y=y)
def set_grid(measure):
global grid
grid = measure