PyQt5: chapter10-显示鼠标坐标

QPainter 类

  • drawLine(): 此方法用于在两组x和y坐标之间绘制线
  • drawlpoints(): 此方法用于在通过提供的x和y坐标指定的位置绘制点
  • drawRect(): 此方法用于在两组x和y坐标之间绘制矩形
  • drawArc(): 此方法用于从指定的中心位置、在两个指定角度之间以及使用指定的半径绘制圆弧
  • drawText(): 此方法用于以指定的字体样式、颜色和大小绘制文本

实例

  1. 创建基于Dialog without Buttons模板窗口
  2. 添加两个Label部件,其text为This app will display x,y coordinates where mouse is moved on
  3. 保存为demoMousetrack.ui
  4. 使用pyuic生成demoMousetrack.py
  5. 创建callMouseTrack.py
import sys
from PyQt5.QtWidgets import QDialog,QApplication
from cookbook_200505.demoMousetrack import *

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui=Ui_Dialog()
        self.setMouseTracking(True)
        self.ui.setupUi(self)
        self.show()
    def mousePressEvent(self, event):
        x=event.x()
        y=event.y()
        text="x: {0},y: {1}".format(x,y)
        self.ui.label.setText(text)
if __name__=="__main__":
    app=QApplication(sys.argv)
    w=MyForm()
    w.show()
    sys.exit(app.exec())

猜你喜欢

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