-
Notifications
You must be signed in to change notification settings - Fork 1
/
sc.py
54 lines (44 loc) · 1.84 KB
/
sc.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
import sys
from PyQt6.QtCore import Qt, QRect
from PyQt6.QtGui import QPainter, QPen
from PyQt6.QtWidgets import QApplication, QMainWindow
class ScreenCapture(QMainWindow):
def __init__(self):
super().__init__()
self.start_pos = None
self.end_pos = None
self.tmp_img_file = '.latest.screenshot.png'
self.setWindowFlags(Qt.WindowType.FramelessWindowHint)
self.setGeometry(0, 0, QApplication.primaryScreen().size().width(), QApplication.primaryScreen().size().height())
self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground, True)
self.rect = None
self.flag = True
self.right_mouse_pressed = False
self.image_hash = None
self.show()
def mousePressEvent(self, event):
self.start_pos = event.pos()
if event.button() == Qt.MouseButton.RightButton:
self.right_mouse_pressed = True
def mouseReleaseEvent(self, event):
if self.right_mouse_pressed and self.rect:
if self.flag:
screen = QApplication.primaryScreen()
pixmap = screen.grabWindow(0, self.rect.x(), self.rect.y(), self.rect.width(), self.rect.height())
pixmap.save(self.tmp_img_file)
self.flag = False
self.close()
def paintEvent(self,event):
if self.rect and self.right_mouse_pressed and self.flag:
qp = QPainter(self)
qp.setPen(QPen(Qt.GlobalColor.red, 2, Qt.PenStyle.SolidLine))
qp.setBrush(Qt.GlobalColor.transparent)
qp.drawRect(self.rect)
def mouseMoveEvent(self, event):
if self.start_pos is not None:
self.rect = QRect(self.start_pos, event.pos())
self.update()
if __name__ == "__main__":
app = QApplication(sys.argv)
sc = ScreenCapture()
sys.exit(app.exec())