Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt5.QtGui import *
- from PyQt5.QtCore import *
- from PyQt5.QtWidgets import *
- import sys
- class Labella(QLabel):
- def __init__(self, parent):
- super().__init__(parent=parent)
- self.flag = False
- pixmap = QPixmap('images.jpg')
- self.setPixmap(pixmap)
- self.resize(pixmap.width(), pixmap.height())
- def paintEvent(self, e):
- if self.flag:
- q = QPainter() # Painting the line
- q.begin(self)
- q.drawRect(self.firstX, self.firstY, self.x - self.firstX, self.y - self.firstY)
- q.end()
- self.update()
- def mousePressEvent(self, e):
- self.flag = True
- self.firstX = e.x()
- self.firstY = e.y()
- def mouseMoveEvent(self, e):
- self.x = e.x()
- self.y = e.y()
- class MouseTracker(QMainWindow):
- distance_from_center = 0
- def __init__(self):
- super().__init__()
- self.test = 0
- self.x = -1
- self.y = -1
- self.firstX = -1
- self.firstY = -1
- self.initUI()
- def initUI(self):
- self.setGeometry(200, 200, 1000, 500)
- self.setWindowTitle('Mouse Tracker')
- lbl = Labella(self)
- self.show()
- def mousePressEvent(self, e):
- self.firstX = e.x()
- self.firstY = e.y()
- def mouseMoveEvent(self, event):
- self.x = event.x()
- self.y = event.y()
- self.update()
- def paintEvent(self, e):
- if not (self.firstX == -1 or self.firstY == -1):
- q = QPainter() # Painting the line
- q.begin(self)
- q.drawRect(self.firstX, self.firstY, abs(self.x - self.firstX), abs(self.y - self.firstY))
- q.end()
- app = QApplication(sys.argv)
- ex = MouseTracker()
- sys.exit(app.exec_())
- from PyQt5.QtGui import *
- from PyQt5.QtCore import *
- from PyQt5.QtWidgets import *
- class Labella(QLabel):
- def __init__(self):
- super().__init__()
- self.flag = False
- pixmap = QPixmap('images.jpg')
- self.setPixmap(pixmap)
- self.resize(pixmap.size())
- self.x = 0
- self.y = 0
- self.firstX = 0
- self.firstY = 0
- def paintEvent(self, e):
- super().paintEvent(e)
- if self.flag:
- painter = QPainter(self)
- painter.setPen(Qt.red)
- painter.setBrush(Qt.red)
- painter.drawRect(self.firstX, self.firstY, self.x - self.firstX, self.y - self.firstY)
- def mousePressEvent(self, e):
- self.flag = True
- self.firstX = e.x()
- self.firstY = e.y()
- def mouseReleaseEvent(self, e):
- self.flag = False
- def mouseMoveEvent(self, e):
- self.x = e.x()
- self.y = e.y()
- self.update()
- if __name__ == '__main__':
- app = QApplication([])
- mw = Labella()
- mw.show()
- app.exec()
Add Comment
Please, Sign In to add comment