-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.py
More file actions
86 lines (73 loc) · 2.93 KB
/
MainWindow.py
File metadata and controls
86 lines (73 loc) · 2.93 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
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter, QPen, QBrush, QColor
from PyQt5.QtCore import Qt, QRect, QPoint, QTimer
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.square_size = 50
self.dragging = False
self.transparent = False
self.square_x = (self.screen().size().width() / 2) - (self.square_size / 2)
self.square_y = (self.screen().size().height() / 2) - (self.square_size / 2)
self.setStyleSheet("background-color: black;")
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 800, 600)
self.setAttribute(Qt.WA_AlwaysStackOnTop)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.showFullScreen()
def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
self.drawSquare(qp)
qp.end()
def drawSquare(self, qp):
brush = QBrush(QColor(255, 255, 255))
brush.setStyle(Qt.SolidPattern)
qp.setPen(QPen(Qt.NoPen))
qp.setBrush(brush)
qp.drawRect(QRect(int(self.square_x), int(self.square_y), int(self.square_size), int(self.square_size)))
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.dragging = True
self.start_drag_x = event.x() - self.square_x
self.start_drag_y = event.y() - self.square_y
def mouseMoveEvent(self, event):
if self.dragging:
self.square_x = event.x() - self.start_drag_x
self.square_y = event.y() - self.start_drag_y
self.update()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.dragging = False
def wheelEvent(self, event):
num_degrees = event.angleDelta().y() / 8
num_steps = num_degrees / 15
self.square_size += num_steps
self.update()
def resizeEvent(self, event):
super().resizeEvent(event)
self.update()
def keyPressEvent(self, event):
if event.key() == Qt.Key_R:
self.square_x = (self.screen().size().width() / 2) - (self.square_size / 2)
self.square_y = (self.screen().size().height() / 2) - (self.square_size / 2)
self.update()
if event.key() == Qt.Key_F:
if self.transparent:
self.setAttribute(Qt.WA_TranslucentBackground, False)
self.setStyleSheet("background-color: black;")
else:
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.setStyleSheet("background-color: transparent;")
self.transparent = not self.transparent
if event.key() == Qt.Key_Escape:
self.close()
def main():
app = QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()