-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMouseEvents.py
More file actions
108 lines (78 loc) · 4.5 KB
/
MouseEvents.py
File metadata and controls
108 lines (78 loc) · 4.5 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
from PyQt6.QtWidgets import QMainWindow, QLabel, QApplication
from PyQt6.QtGui import QIcon
from PyQt6.QtCore import QSize, Qt, QTimer
import ctypes
# This code makes it so windows doesn't think Pythonw.exe's icon should be used for this window's icon
myappid = u'mycompany.myproduct.subproduct.version' #arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
class MainWindow(QMainWindow): #<- Boilerplate
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs) #<- till here
self.setWindowIcon(QIcon("icons\\Icon.png")) #assigning the application/window an icon for the taskbar and window
self.setWindowTitle("Mouse Events")
self.setMinimumSize(QSize(720, 480)) #setting a minimum size for the window (can't make it smaller)
self.label = QLabel("Click in this window") #putting text in middle of screen
self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.font = self.label.font() #increasing the text size and making it bold using setFont()
self.font.setPointSize(18)
self.font.setBold(True)
self.label.setFont(self.font)
self.timer = QTimer()
self.setCentralWidget(self.label)
def appendText(self, textToAppend):
currText = self.label.text()
self.label.setText("\n".join([currText, textToAppend]))
def mouseMoveEvent(self, e):
self.label.setText("Oooo moving your mouse...\nnaughty naughty...")
self.label.setStyleSheet("color:blue")
if self.timer.isActive(): #deactivating the timer if mouse is not idle
self.timer.stop()
if e.button() == Qt.MouseButton.LeftButton:
self.appendText("Left Button Pressed")
elif e.button() == Qt.MouseButton.RightButton:
self.appendText("Right Button Pressed")
elif e.button() == Qt.MouseButton.MiddleButton:
self.appendText("Middle Button Pressed")
if e.button() == Qt.MouseButton.NoButton:
self.appendText("No Button Pressed")
def mousePressEvent(self, e):
self.label.setText("Hello i am pressing my mouse, here too much raining\nOOoooUUuuuOOoooo")
self.label.setStyleSheet("color:purple")
if self.timer.isActive(): #deactivating the timer if mouse is not idle
self.timer.stop()
if e.button() == Qt.MouseButton.LeftButton:
self.appendText("Left Button Pressed")
elif e.button() == Qt.MouseButton.RightButton:
self.appendText("Right Button Pressed")
elif e.button() == Qt.MouseButton.MiddleButton:
self.appendText("Middle Button Pressed")
def mouseReleaseEvent(self, e):
self.label.setText("UNHAND ME YOU FIEND")
self.label.setStyleSheet("color:red")
self.timer.setInterval(3000) #Setting a timer to only time out when mouse has been released
self.timer.timeout.connect(self.resetWindow)#(when the mouse is sitting idle)
self.timer.start()
if e.button() == Qt.MouseButton.LeftButton:
self.appendText("Left Button Released")
elif e.button() == Qt.MouseButton.RightButton:
self.appendText("Right Button Released")
elif e.button() == Qt.MouseButton.MiddleButton:
self.appendText("Middle Button Released")
def mouseDoubleClickEvent(self, e):
self.label.setText("Oh my.. GAAAAAAHHHHHHH u press me 2 tems")
self.label.setStyleSheet("color:green")
if self.timer.isActive(): #deactivating the timer if mouse is not idle
self.timer.stop()
if e.button() == Qt.MouseButton.LeftButton:
self.appendText("Left Button Pressed")
elif e.button() == Qt.MouseButton.RightButton:
self.appendText("Right Button Pressed")
elif e.button() == Qt.MouseButton.MiddleButton:
self.appendText("Middle Button Pressed")
def resetWindow(self):
self.label.setText("Click in this window")
self.label.setStyleSheet("color:black")
app = QApplication([]) #initializing the application
window = MainWindow()
window.show() #Windows are made invisible by default, need show func to show them
app.exec() #executing the program