Guest User

Untitled

a guest
Jul 16th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. from PyQt5.QtGui import *
  2. from PyQt5.QtCore import *
  3. from PyQt5.QtWidgets import *
  4. import sys
  5.  
  6. class Labella(QLabel):
  7. def __init__(self, parent):
  8. super().__init__(parent=parent)
  9. self.flag = False
  10. pixmap = QPixmap('images.jpg')
  11. self.setPixmap(pixmap)
  12. self.resize(pixmap.width(), pixmap.height())
  13.  
  14. def paintEvent(self, e):
  15. if self.flag:
  16. q = QPainter() # Painting the line
  17.  
  18. q.begin(self)
  19.  
  20. q.drawRect(self.firstX, self.firstY, self.x - self.firstX, self.y - self.firstY)
  21.  
  22. q.end()
  23. self.update()
  24.  
  25. def mousePressEvent(self, e):
  26. self.flag = True
  27. self.firstX = e.x()
  28. self.firstY = e.y()
  29.  
  30. def mouseMoveEvent(self, e):
  31. self.x = e.x()
  32. self.y = e.y()
  33.  
  34.  
  35. class MouseTracker(QMainWindow):
  36. distance_from_center = 0
  37. def __init__(self):
  38. super().__init__()
  39. self.test = 0
  40. self.x = -1
  41. self.y = -1
  42. self.firstX = -1
  43. self.firstY = -1
  44. self.initUI()
  45.  
  46.  
  47. def initUI(self):
  48. self.setGeometry(200, 200, 1000, 500)
  49. self.setWindowTitle('Mouse Tracker')
  50.  
  51. lbl = Labella(self)
  52.  
  53. self.show()
  54.  
  55. def mousePressEvent(self, e):
  56. self.firstX = e.x()
  57. self.firstY = e.y()
  58.  
  59. def mouseMoveEvent(self, event):
  60. self.x = event.x()
  61. self.y = event.y()
  62.  
  63. self.update()
  64.  
  65. def paintEvent(self, e):
  66. if not (self.firstX == -1 or self.firstY == -1):
  67. q = QPainter() # Painting the line
  68.  
  69. q.begin(self)
  70.  
  71. q.drawRect(self.firstX, self.firstY, abs(self.x - self.firstX), abs(self.y - self.firstY))
  72.  
  73. q.end()
  74.  
  75.  
  76. app = QApplication(sys.argv)
  77. ex = MouseTracker()
  78. sys.exit(app.exec_())
  79.  
  80. from PyQt5.QtGui import *
  81. from PyQt5.QtCore import *
  82. from PyQt5.QtWidgets import *
  83.  
  84.  
  85. class Labella(QLabel):
  86. def __init__(self):
  87. super().__init__()
  88. self.flag = False
  89.  
  90. pixmap = QPixmap('images.jpg')
  91. self.setPixmap(pixmap)
  92. self.resize(pixmap.size())
  93.  
  94. self.x = 0
  95. self.y = 0
  96. self.firstX = 0
  97. self.firstY = 0
  98.  
  99. def paintEvent(self, e):
  100. super().paintEvent(e)
  101.  
  102. if self.flag:
  103. painter = QPainter(self)
  104.  
  105. painter.setPen(Qt.red)
  106. painter.setBrush(Qt.red)
  107.  
  108. painter.drawRect(self.firstX, self.firstY, self.x - self.firstX, self.y - self.firstY)
  109.  
  110. def mousePressEvent(self, e):
  111. self.flag = True
  112. self.firstX = e.x()
  113. self.firstY = e.y()
  114.  
  115. def mouseReleaseEvent(self, e):
  116. self.flag = False
  117.  
  118. def mouseMoveEvent(self, e):
  119. self.x = e.x()
  120. self.y = e.y()
  121.  
  122. self.update()
  123.  
  124.  
  125. if __name__ == '__main__':
  126. app = QApplication([])
  127.  
  128. mw = Labella()
  129. mw.show()
  130.  
  131. app.exec()
Add Comment
Please, Sign In to add comment