PyQt5: chapter10-display coordinates of click and release of mouse button

Instance

  1. Create a template window based on Dialog without Buttons
  2. Add three Label parts, the text is Displays the x,y coordinates where mouse is pressed and released
  3. Set the objectName of the second Label component to labelPress
  4. Set the objectName of the third Label component to labelRelease
  5. Save as demoMouseClicks.ui
  6. Use pyuic to generate demoMouseClicks.py
  7. Create callMouseClickCoordinates.py
import sys
from PyQt5.QtWidgets import QDialog,QApplication
from cookbook_200505.demoMouseClicks import *

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui=Ui_Dialog()
        self.ui.setupUi(self)
        self.show()
    def mousePressEvent(self, event):
        if event.buttons() & QtCore.Qt.LeftButton:
            x=event.x()
            y=event.y()
            text="x: {0}, y: {1}".format(x,y)
            self.ui.labelRelease.setText("Mouse button pressed at "+text)
    def mouseReleaseEvent(self, event):
        x = event.x()
        y = event.y()
        text = "x: {0}, y: {1}".format(x, y)
        self.ui.labelRelease.setText("Mouse button relrased at " + text)
        self.update()
if __name__=="__main__":
    app=QApplication(sys.argv)
    w=MyForm()
    w.show()
    sys.exit(app.exec())

 

Guess you like

Origin blog.csdn.net/weixin_43307431/article/details/105935520