forked from PantsuDango/Dango-Translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchooseRange.py
135 lines (106 loc) · 4.16 KB
/
chooseRange.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
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
import json
from traceback import print_exc
class Range(QMainWindow):
def __init__(self, X1, Y1, X2, Y2):
try :
super(Range, self).__init__()
self.setGeometry(X1, Y1, X2-X1, Y2-Y1)
# 窗口无标题栏、窗口置顶、窗口透明
self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint | Qt.Tool)
self.setAttribute(Qt.WA_TranslucentBackground)
self.Label = QLabel(self)
self.Label.setObjectName("dragLabel")
self.Label.setGeometry(0, 0, X2-X1, Y2-Y1)
self.Label.setStyleSheet("border-width:1;\
border:2px dashed #1E90FF;\
background-color:rgba(62, 62, 62, 0.01)")
# 此Label用于当鼠标进入界面时给出颜色反应
self.dragLabel = QLabel(self)
self.dragLabel.setObjectName("dragLabel")
self.dragLabel.setGeometry(0, 0, 4000, 2000)
self.Font = QFont()
self.Font.setFamily("华康方圆体W7")
self.Font.setPointSize(12)
# 隐藏按钮
self.Button = QPushButton(self)
self.Button.setGeometry(QRect(X2-X1-40, 0, 40, 30))
self.Button.setStyleSheet("background-color:rgba(62, 62, 62, 0.3);"
"color:#1E90FF")
self.Button.setFont(self.Font)
self.Button.setText("隐藏")
self.Button.setCursor(QCursor(Qt.PointingHandCursor))
self.Button.clicked.connect(self.close)
self.Button.hide()
# 右下角用于拉伸界面的控件
self.statusbar = QStatusBar(self)
self.setStatusBar(self.statusbar)
except Exception:
print_exc()
# 鼠标移动事件
def mouseMoveEvent(self, e: QMouseEvent):
try :
self._endPos = e.pos() - self._startPos
self.move(self.pos() + self._endPos)
except Exception :
print_exc()
# 鼠标按下事件
def mousePressEvent(self, e: QMouseEvent):
try :
if e.button() == Qt.LeftButton:
self._isTracking = True
self._startPos = QPoint(e.x(), e.y())
except Exception :
print_exc()
# 鼠标松开事件
def mouseReleaseEvent(self, e: QMouseEvent):
try :
if e.button() == Qt.LeftButton:
self._isTracking = False
self._startPos = None
self._endPos = None
except Exception :
print_exc()
# 鼠标进入控件事件
def enterEvent(self, QEvent):
try :
rect = self.geometry()
X1 = rect.left()
Y1 = rect.top()
X2 = rect.left() + rect.width()
Y2 = rect.top() + rect.height()
self.Button.setGeometry(QRect(X2 - X1 - 40, 0, 40, 30))
self.Button.show()
self.dragLabel.setStyleSheet("background-color:rgba(62, 62, 62, 0.3)")
except Exception:
print_exc()
# 鼠标离开控件事件
def leaveEvent(self, QEvent):
try :
self.dragLabel.setStyleSheet("background-color:none")
self.Label.setGeometry(0, 0, self.width(), self.height())
self.Button.hide()
rect = self.geometry()
X1 = rect.left()
Y1 = rect.top()
X2 = rect.left() + rect.width()
Y2 = rect.top() + rect.height()
with open('.\\config\\settin.json') as file:
data = json.load(file)
data["range"]["X1"] = X1
data["range"]["Y1"] = Y1
data["range"]["X2"] = X2
data["range"]["Y2"] = Y2
with open('.\\config\\settin.json', 'w') as file:
json.dump(data, file)
except Exception :
print_exc()
if __name__ == '__main__':
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = Range(500, 500, 1000, 600)
win.show()
app.exec_()