-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.py
121 lines (103 loc) · 3.6 KB
/
main.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
import os
import tkinter as tk
from pynput import mouse
from components.input_range import InputRange
# 组件层
from components.radio import Radio
from components.switch_button import SwitchButton
# 业务层
from service.settings import Colors
from service.image_cache import ImageCache
from service.click_tracker import ClickTracker
from service.move_tracker import MoveTracker
from service.trackers import Trackers
# 通用工具层
from utils.get_screen_size import get_main_screen_size
import subprocess
import sys
from tkinter import messagebox
from PIL import Image, ImageTk
def open_out_folder():
"""打开输出图片的位置"""
out_path = os.path.dirname("./out")
if sys.platform.startswith("darwin"):
subprocess.run(['open', out_path])
elif sys.platform.startswith("win32"):
os.startfile(out_path)
elif sys.platform.startswith("win64"):
os.startfile(out_path)
elif sys.platform.startswith("linux"):
subprocess.run(['xdg-open', out_path])
else:
messagebox.showinfo("提示", "无法打开文件夹,请手动打开")
class App(tk.Tk):
def __init__(self):
super(App, self).__init__()
# 配置ui信息
self.title("Mouse Tracker")
self.geometry("400x400")
icon_path = './assert/a952d-5afjj.icns'
if sys.platform.startswith("darwin"):
icon_image = ImageTk.PhotoImage(Image.open(icon_path))
self.iconphoto(True, icon_image)
else:
self.wm_iconbitmap('./assert/favicon.ico')
self.config(
background='#2e3e26',
)
# 绑定数据信息
self.imageCache = ImageCache(
get_main_screen_size()
)
self.line_opacity_value = tk.IntVar(value=50)
self.line_opacity = InputRange(
'线条不透明度(实时)',
variable=self.line_opacity_value,
from_=1,
to=100,
)
self.line_width_value = tk.IntVar(value=2)
self.line_width = InputRange(
'线条宽度(实时)',
variable=self.line_width_value,
from_=1,
to=25,
)
self.trackers = Trackers(
click_trackers={
mouse.Button.left: ClickTracker(cache=self.imageCache, color=Colors.Left),
mouse.Button.right: ClickTracker(cache=self.imageCache, color=Colors.Right),
mouse.Button.middle: ClickTracker(
cache=self.imageCache, color=Colors.Middle
),
},
move_tracker=MoveTracker(self.imageCache, self.line_opacity_value, self.line_width_value),
)
# 挂载组件
self.radio_list = [
Radio(self, text=text, variable=tracker)
for text, tracker in zip(
["记录左键点击位置", "记录右键点击位置", "记录中键点击位置", "记录鼠标移动轨迹"],
[*self.trackers.click_trackers.values(), self.trackers.move_tracker],
)
]
self.switch_button = SwitchButton(
[self.start_tracking, self.stop_tracking],
['开始记录', '结束记录'],
)
self.open_out_files = SwitchButton(
[open_out_folder], ['打开out文件夹']
)
def start_tracking(self):
"""点击开始记录"""
self.trackers.reset()
self.trackers.start()
def stop_tracking(self):
"""点击结束记录"""
self.imageCache.save()
self.trackers.stop()
def main():
app = App()
app.mainloop()
if __name__ == "__main__":
main()