Software and hardware environment
- Windows 10 64bit
- Anaconda3 with python 3.8
- PyQt5 5.15
Introduction
The mouse is an important means of interface interaction. In this article, we will take a look at several common mouse events in PyQt5
and their sample codes.
click event
The mouse click is actually the process of mouse press -> mouse release , and the corresponding mouse events are mousePressEvent
and mouseReleaseEvent
, so you only need to rewrite these two methods in the window. In addition, you can judge whether it is the left mouse button or the right mouse button by judging the button
method of the event
.
def mousePressEvent(self, event): print('mousePressEvent') if event.button() == Qt.LeftButton: print('left') elif event.button() == Qt.RightButton: print('right') elif event.button() == Qt.MiddleButton: print('middle') def mouseReleaseEvent(self, event): print('mouseReleaseEvent')
If you want to get the coordinates of the mouse click position, you can get it through the related methods of event
, see the following code snippet
# 相对于窗口的位置x = event.x() y = event.y() # xy类型是QPoint,QPoint的x()和y()方法就是x和y的值xy = event.pos() print('x={}, y={}, xy={}, x={}, y={}'.format(x, y, xy, xy.x(), xy.y())) # 相对于屏幕的位置x_global = event.globalX() y_global = event.globalY() # xy_global类型是QPoint xy_global = event.globalPos() print('x_global={}, y_global={}, xy_global={}'.format(x_global, y_global, xy_global))
double click event
The double-click of the mouse is actually the process of double-click -> release of the mouse. The corresponding mouse events are mouseDoubleClickEvent
and mouseReleaseEvent
, so you only need to rewrite these two methods in the window.
def mouseDoubleClickEvent(self, event): print('mouseDoubleClickEvent') def mouseReleaseEvent(self, event): print('mouseReleaseEvent')
mouse movement
Add mouse movement to the click event, mouse click -> hold down the mouse and move -> mouse release , the corresponding mouse events are mousePressEvent
, mouseMoveEvent
and mouseReleaseEvent
, so you only need to override these three methods in the window. Can
def mousePressEvent(self, event): print('mousePressEvent') def mouseMoveEvent(self, event): print('mouseMoveEvent') def mouseReleaseEvent(self, event): print('mouseReleaseEvent')
wheel event
Mouse wheel event needs to override wheelEvent
def wheelEvent(self, event): print('wheelEvent')
Cursor in and out of window events
This cursor enters and exits the window, in fact, whether the focus is on the form, you need to override the methods enterEvent
and leaveEvent
def enterEvent(self, event): print('enterEvent') def leaveEvent(self, event): print('leaveEvent')
window close event
Here, the window closing event is also discussed in the mouse event. It is also a function that is often used in actual development. Here we need to rewrite the closeEvent
method.
def closeEvent(self, event): """重写closeEvent 方法,实现在窗口关闭时去执行一些功能""" reply = QMessageBox.question(self, '这是标题', "这是消息体", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: # 关闭窗口event.accept() else: # 取消event.ignore()
Source code download
https://github.com/xugaoxiang/learningPyQt5
PyQt5 series of tutorials
For more PyQt5
tutorials, please move
https://xugaoxiang.com/category/python/pyqt5/
This article is reprinted from https://xugaoxiang.com/2022/05/15/pyqt5-28-mouse-event/
This site is for inclusion only, and the copyright belongs to the original author.