code: def paintEvent(self, event):
          
          painter = QPainter(self)
          painter.fillRect(event.rect(), self.palette().color(QPalette.Window))
          painter.setPen(Qt.black)
  
          block = self._qpart.firstVisibleBlock()
          blockNumber = block.blockNumber()
          top = int(self._qpart.blockBoundingGeometry(block).translated(self._qpart.contentOffset()).top())
          bottom = top + int(self._qpart.blockBoundingRect(block).height())
          singleBlockHeight = self._qpart.cursorRect().height()
  
          boundingRect = self._qpart.blockBoundingRect(block)
          availableWidth = self.__width - self._RIGHT_MARGIN - self._LEFT_MARGIN
          availableHeight = self._qpart.fontMetrics().height()
          while block.isValid() and top <= event.rect().bottom():
              if block.isVisible() and bottom >= event.rect().top():
                  number = str(blockNumber + 1)
                  painter.drawText(self._LEFT_MARGIN, top,
                                   availableWidth, availableHeight,
                                   Qt.AlignRight, number)
                  if boundingRect.height() >= singleBlockHeight * 2:  # wrapped block
                      painter.fillRect(1, top + singleBlockHeight,
                                       self.__width - 2, boundingRect.height() - singleBlockHeight - 2,
                                       Qt.darkGreen)
  
              block = block.next()
              boundingRect = self._qpart.blockBoundingRect(block)
              top = bottom
              bottom = top + int(boundingRect.height())
              blockNumber += 1
docs: QWidget.paintEvent() implementation