先上代碼,主要看鼠標(biāo)移動事件里面代碼
import sys
from PyQt5 import QtCore
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtGui import QPainter, QPen, QPixmap, QPainterPath, QFont, QColor, QBrush
from PyQt5.QtWidgets import QApplication, QWidget
import numpy as np
class DemoMouseEvent(QWidget):
def __init__(self, parent=None):
super(DemoMouseEvent, self).__init__(parent)
# 設(shè)置窗口標(biāo)題
self.setWindowTitle('鼠標(biāo)事件演示')
# 設(shè)置窗口大小
self.setFixedSize(480, 320)
self.beginPoint = QPoint() # 起始點(diǎn)
self.endPoint = QPoint() # 結(jié)束點(diǎn)
self.pixmap = QPixmap('d:\\67.jpg')
#self.pixmap.fill(Qt.lightGray)
self.copt_pixmap =self.pixmap.copy()
self.setMouseTracking(True)
self.cur_x = 0
self.cur_y = 0
def draw(self, painter):
path = QPainterPath()
f = QFont('黑體', 10)
point = QPoint(20, 20)
path.addText(point, f, "世界您好")
point1 = QPoint(250, 250)
point2 = QPoint(self.cur_x, self.cur_y)
path.moveTo(point)
path.lineTo(point1)
path.lineTo(point2)
path.lineTo(point)
#path.addEllipse(point, 5, 5)
painter.fillPath(path, QColor(0, 255, 0, 128))
painter.drawPath(path)
# 重繪窗口事件
def paintEvent(self, event):
self.pixmap=self.copt_pixmap.copy()
pp = QPainter(self.pixmap)
pp.setPen(QPen(Qt.blue, 2)) # 設(shè)置畫筆
self.draw(pp)
# 繪制直線
pp.drawLine(self.beginPoint, self.endPoint)
# 上一直線的終點(diǎn)就是下一直線的起點(diǎn)
self.beginPoint = self.endPoint
# 在畫布上畫出
painter = QPainter(self)
painter.drawPixmap(0, 0, self.pixmap)
def wheelEvent(self, ev):
mods = ev.modifiers()
# print('mods=', mods)
delta = ev.angleDelta()
# print('delta=', delta)
if QtCore.Qt.ControlModifier == int(mods):
if int(delta.y()) > 0:
print("ctrl 向上滾輪")
else:
print("ctrl 向下滾輪")
def mousePressEvent(self, event):
# 鼠標(biāo)左鍵按下
if event.button() == Qt.LeftButton:
self.startPoint = event.pos()
def mouseReleaseEvent(self, event):
# 鼠標(biāo)左鍵釋放
if event.button() == Qt.LeftButton:
self.endPoint = event.pos()
# 重新繪制
self.update()
def mouseMoveEvent(self, event):
tmp1 = event.y()
tmp2 = event.localPos().y()
tmp3 = event.pos().y()
tmp4 = event.windowPos().y()
tmp5 = event.y() + self.y()
tmp6 = event.screenPos().y()
tmp7 = event.globalPos().y()
print("所有點(diǎn)的位置:")
print(tmp1)
print(tmp2)
print(tmp3)
print(tmp4)
print(tmp5)
print(tmp6)
print(tmp7)
# 鼠標(biāo)左鍵按下的同時移動鼠標(biāo)
if event.buttons() and Qt.LeftButton:
self.endPoint = event.pos()
# 重新繪制
self.update()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = DemoMouseEvent()
window.show()
sys.exit(app.exec())
通過運(yùn)行代碼可以發(fā)現(xiàn):文章來源:http://www.zghlxwxcb.cn/news/detail-551004.html
tmp1 = event.y() tmp2 = event.localPos().y() tmp3 = event.pos().y() tmp4 = event.windowPos().y() 上面四個是相對于父控件的坐標(biāo),搞編程都知道圖像坐標(biāo)是x軸是圖像上面,y軸是圖像左側(cè) tmp5 = event.y() + self.y() 上面這個是相對于窗口坐標(biāo) tmp6 = event.screenPos().y() tmp7 = event.globalPos().y()?
上面這2行就是相當(dāng)于屏幕左上角坐標(biāo)了文章來源地址http://www.zghlxwxcb.cn/news/detail-551004.html
到了這里,關(guān)于[pyqt5]關(guān)于在pyqt5界面上鼠標(biāo)位置問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!