How to return mouse coordinates in realtime?

后端 未结 1 803
[愿得一人]
[愿得一人] 2021-01-19 23:57

I\'m new to PyQt and I\'m trying to use it to create a widget that returns the position of the mouse in real time.

Here\'s what I have:

import sys
fr         


        
相关标签:
1条回答
  • 2021-01-20 00:21

    The QMouseEvent function must be implemented since it is executed when the mouse is moved.

    import sys
    from PyQt5.QtWidgets import (QApplication, QLabel, QWidget)
    
    
    class MouseTracker(QWidget):
        def __init__(self):
            super().__init__()
            self.initUI()
            self.setMouseTracking(True)
    
        def initUI(self):
            self.setGeometry(300, 300, 300, 200)
            self.setWindowTitle('Mouse Tracker')
            self.label = QLabel(self)
            self.label.resize(200, 40)
            self.show()
    
        def mouseMoveEvent(self, event):
            self.label.setText('Mouse coords: ( %d : %d )' % (event.x(), event.y()))
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = MouseTracker()
        sys.exit(app.exec_())
    

    0 讨论(0)
提交回复
热议问题