PyQt5 模拟镜头
2019-10-06 python 1858
import sys from PyQt5.QtWidgets import QWidget, QApplication, QDesktopWidget from PyQt5.QtCore import Qt, QTimer from PyQt5.QtGui import QPainter, QColor, QPen, QCursor, QFont, QBrush class RectPos: x1 = 0 y1 = 0 x2 = 0 y2 = 0 class Rect(QWidget): __dragWin = False def __init__(self): super().__init__() self.initUI() def initUI(self): self.setMouseTracking(True) # 保证得到鼠标信息 # pyqt5设置窗体透明控件不透明 # self.setAttribute(Qt.WA_TranslucentBackground) self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint | Qt.Tool) # self.setWindowOpacity(.8) self.time = 0 self.timer = QTimer() self.timer.setInterval(10) self.timer.timeout.connect(self.update) self.timer.start() self.rect = RectPos() self.setGeometry(300, 300, 500, 400) self.setWindowTitle("绘画") self.center() def paintEvent(self, ev): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) ## 抗锯齿 # painter.save() pen = QPen(QColor(166, 66, 250), 1, Qt.SolidLine) painter.setPen(pen) painter.drawRect(0, 0, self.width(), self.height()) w = self.width() h = self.height() space = 30 # 镜头 线条 painter.drawLine(space, space, space * 2, space) painter.drawLine(space, space, space, space * 2) painter.drawLine(w - space * 2, space, w - space, space) painter.drawLine(w - space, space, w - space, space * 2) painter.drawLine(space, h - space * 2, space, h - space) painter.drawLine(space, h - space, space * 2, h - space) painter.drawLine(w - space, h - space * 2, w - space, h - space) painter.drawLine(w - space, h - space, w - space * 2, h - space) brush = QBrush(Qt.SolidPattern) brush.setColor(QColor("#0000ff")) painter.setBrush(brush) painter.drawEllipse(w - 65, 85, 10, 10) painter.setFont(QFont("SimSun", 24)) painter.drawText(w - 120, 100, "REC") painter.setFont(QFont("SimSun", 16)) painter.drawText(w / 2 - 130, h / 2, "pos:(%d,%d,%d,%d)" % (self.x(), self.y(), self.x() + self.width(), self.y() + self.height())) painter.drawText(w / 2 - 70, h - 50, "双击获取坐标") def mousePressEvent(self, e): self.__dragWin = True self.__dragWin_x = e.x() self.__dragWin_y = e.y() self.rect.x1 = self.x() self.rect.y1 = self.y() self.rect.x2 = self.x() + self.width() self.rect.y2 = self.y() + self.height() self.setCursor(QCursor(Qt.SizeAllCursor)) # 更改鼠标图标 def mouseMoveEvent(self, e): pos = e.globalPos() x = e.x() y = e.y() space = 20 if x < space and y < space: self.setCursor(QCursor(Qt.CrossCursor)) # 更改鼠标图标 if self.__dragWin == True: if abs(self.rect.x2 - self.x()) > 0 and abs(self.rect.y2 - self.y()) > 0: self.setGeometry(pos.x(), pos.y(), abs(self.rect.x2 - self.x()), abs(self.rect.y2 - self.y())) elif x > self.width() - space and y < space: self.setCursor(QCursor(Qt.CrossCursor)) # 更改鼠标图标 if self.__dragWin == True: if x > 0 and abs(self.rect.y2 - self.y()) > 0: self.setGeometry(self.x(), pos.y(), x, abs(self.rect.y2 - self.y())) elif x < space and y > self.height() - space: # 左下角 self.setCursor(QCursor(Qt.CrossCursor)) # 更改鼠标图标 if self.__dragWin == True: if abs(self.rect.x2 - self.x()) > 0 and abs(pos.y() - self.rect.y1) > 0: self.setGeometry(pos.x(), self.y(), abs(self.rect.x2 - self.x()), abs(pos.y() - self.rect.y1)) elif x > self.width() - space and y > self.height() - space: self.setCursor(QCursor(Qt.CrossCursor)) # 更改鼠标图标 if self.__dragWin == True: if x > 0 and abs(pos.y() - self.y()) > 0: self.setGeometry(self.x(), self.y(), x, abs(pos.y() - self.y())) elif x < space: self.setCursor(QCursor(Qt.SizeHorCursor)) # 更改鼠标图标 if self.__dragWin == True: if abs(self.rect.x2 - pos.x()) > 0: self.setGeometry(pos.x(), self.y(), abs(self.rect.x2 - pos.x()), self.height()) elif x > self.width() - space: self.setCursor(QCursor(Qt.SizeHorCursor)) # 更改鼠标图标 if self.__dragWin == True: if x > 0: self.setGeometry(self.x(), self.y(), x, self.height()) elif y < space: self.setCursor(QCursor(Qt.SizeVerCursor)) # 更改鼠标图标 if self.__dragWin == True: if abs(self.rect.y2 - pos.y()) > 0: self.setGeometry(self.x(), pos.y(), self.width(), abs(self.rect.y2 - pos.y())) elif y > self.height() - space: self.setCursor(QCursor(Qt.SizeVerCursor)) # 更改鼠标图标 if self.__dragWin == True: if abs(pos.y() - self.y()): self.setGeometry(self.x(), self.y(), self.width(), abs(pos.y() - self.y())) else: self.setCursor(QCursor(Qt.SizeAllCursor)) # 更改鼠标图标 if self.__dragWin == True: self.move(pos.x() - self.__dragWin_x, pos.y() - self.__dragWin_y) def mouseReleaseEvent(self, e): self.__dragWin = False self.setCursor(QCursor(Qt.ArrowCursor)) def mouseDoubleClickEvent(self, e): print((self.x(), self.y(), self.x() + self.width(), self.y() + self.height())) QApplication.instance().exit() sys.exit(0) def center(self): qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) if __name__ == "__main__": app = QApplication(sys.argv) line = Rect() line.show() sys.exit(app.exec_())
很赞哦! (0)
相关文章
文章评论
-
-
-
0条评论