-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen_select_basic.py
More file actions
124 lines (89 loc) · 2.84 KB
/
screen_select_basic.py
File metadata and controls
124 lines (89 loc) · 2.84 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
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
import tkinter as tk
from PIL import ImageGrab, ImageTk
root = None
canvas = None
background = None
full_image = None
state = {"start_x": 0, "start_y": 0, "rect_id": None}
drag = {"x": 0, "y": 0}
def on_press(event):
state["start_x"] = event.x
state["start_y"] = event.y
if state["rect_id"]:
canvas.delete(state["rect_id"])
state["rect_id"] = None
def on_drag(event):
if state["rect_id"]:
canvas.delete(state["rect_id"])
state["rect_id"] = canvas.create_rectangle(
state["start_x"],
state["start_y"],
event.x,
event.y,
outline="light grey",
width=2,
)
def on_release(event):
x1 = min(state["start_x"], event.x)
y1 = min(state["start_y"], event.y)
x2 = max(state["start_x"], event.x)
y2 = max(state["start_y"], event.y)
root.withdraw()
root.update_idletasks()
image = full_image.crop((x1, y1, x2, y2))
width = x2 - x1
height = y2 - y1
root.attributes("-fullscreen", False)
root.overrideredirect(True)
root.geometry(f"{width}x{height}+{x1}+{y1}")
canvas.destroy()
view = tk.Canvas(root, width=width, height=height, highlightthickness=0, bd=0)
view.pack()
photo = ImageTk.PhotoImage(image)
view.create_image(0, 0, anchor="nw", image=photo)
view.image = photo
view.create_rectangle(1, 1, width - 1, height - 1, outline="black", width=2)
size = max(20, min(width, height) // 6)
offset = size // 2
center_x = width // 2
center_y = height // 2
points = [
center_x - offset // 2,
center_y - offset,
center_x - offset // 2,
center_y + offset,
center_x + offset,
center_y,
]
triangle = view.create_polygon(points, fill="white", outline="black", width=2)
view.tag_bind(triangle, "<Button-1>", close_window)
view.bind("<ButtonPress-1>", start_move)
view.bind("<B1-Motion>", move_window)
root.deiconify()
root.mainloop()
def close_window(_event):
root.destroy()
def start_move(event):
drag["x"] = event.x
drag["y"] = event.y
def move_window(event):
x = event.x_root - drag["x"]
y = event.y_root - drag["y"]
root.geometry(f"+{x}+{y}")
def make_snip():
global root, canvas, background, full_image
full_image = ImageGrab.grab()
root = tk.Tk()
root.attributes("-fullscreen", True)
root.attributes("-topmost", True)
root.bind("<Escape>", close_window)
canvas = tk.Canvas(root, cursor="cross", bg="black", highlightthickness=0)
canvas.pack(fill=tk.BOTH, expand=True)
background = ImageTk.PhotoImage(full_image)
canvas.create_image(0, 0, anchor="nw", image=background)
state["rect_id"] = None
canvas.bind("<ButtonPress-1>", on_press)
canvas.bind("<B1-Motion>", on_drag)
canvas.bind("<ButtonRelease-1>", on_release)
root.mainloop()
make_snip()