PyQt5: chapter10-显示单击和释放鼠标按钮的坐标

实例

  1. 创建基于Dialog without Buttons模板窗口
  2. 添加三个Label部件,其text为Displays the x,y coordinates where mouse is pressed and released
  3. 设定第二个Label部件的objectName为labelPress
  4. 设定第三个Label部件的objectName为labelRelease
  5. 保存为demoMouseClicks.ui
  6. 使用pyuic生成demoMouseClicks.py
  7. 创建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())

猜你喜欢

转载自blog.csdn.net/weixin_43307431/article/details/105935520
今日推荐