title: Broken drawing pencil
body: <p>But in the implementation, I ran into a thing that was not clear to me.</p>
  
  <p>Working pencil implementation code:</p>
  
  <pre><code>import sys
  from PyQt5.QtCore import QPoint, QRect, QSize, Qt
  from PyQt5.QtGui import QPainter, QPen, QIcon, QPixmap, QColor
  from PyQt5.QtWidgets import (QAction, QApplication, QMainWindow,QWidget)
  
  
  class AbstractScribbleArea(QWidget):
      def __init__(self, parent=None):
          super(AbstractScribbleArea, self).__init__(parent)
          self.modified = False
          self._begin = QPoint()
          self._end = QPoint()
          self._lastPoint = QPoint()
          self._pixmap = QPixmap()
          self._shape = ""
  
          self.myPenWidth = 10
          self.myPenColor = QColor("black")
  
      def mousePressEvent(self, event):
          if event.buttons() &amp; Qt.LeftButton:
              self._begin = event.pos()
              self._end = event.pos()
              self.update()
          super(AbstractScribbleArea, self).mousePressEvent(event)
  
      def mouseMoveEvent(self, event):
          if event.buttons() &amp; Qt.LeftButton:
              self._end = event.pos()
              self.update()
          super(AbstractScribbleArea, self).mouseMoveEvent(event)
  
      def mouseReleaseEvent(self, event):
          # draw on image
          painter = QPainter(self._pixmap)
          if self._shape == 'pencil':
              painter.setPen(QPen(self.myPenColor, self.myPenWidth,
                                  Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
          self.draw(painter, self._begin, self._end, True)
          self._begin = QPoint()
          self._end = QPoint()
          self.update()
          super(AbstractScribbleArea, self).mouseReleaseEvent(event)
  
      def paintEvent(self, event):
          painter = QPainter(self)
          painter.drawPixmap(self.rect(), self._pixmap, self.rect())
          if self._shape == 'pencil':
              painter.setPen(QPen(self.myPenColor, self.myPenWidth,
                                  Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
          self.draw(painter, self._begin, self._end, False)
  
      def resizeEvent(self, event):
          w = self.width() if self.width() &gt; self._pixmap.width() else self._pixmap.width()
          h = self.height() if self.height() &gt; self._pixmap.height() else self._pixmap.height()
  
          s = QSize(w, h)
          if s != self._pixmap.size():
              pixmap = QPixmap(self._pixmap)
              self._pixmap = QPixmap(s)
              painter = QPainter(self._pixmap)
              painter.fillRect(QRect(0, 0, w, h), Qt.white)
              painter.drawPixmap(pixmap.rect(), pixmap)
          super(AbstractScribbleArea, self).resizeEvent(event)
  
      def draw(self, painter, _begin, _end, is_released):
          raise NotImplementedError
  
      def sizeHint(self):
          return QSize(640, 480)
  
      def set_shape(self, shape):
          self._shape = shape
  
      def shape(self):
          return self._shape
  
  
  
  class ScribbleArea(AbstractScribbleArea):
      def draw(self, painter, _begin, _end, is_released):
          if _begin.isNull() or _end.isNull():
              return
          elif self.shape() == "pencil":
              self.drawPencil(painter, _begin, _end)
  
      def drawPencil(self, painter, _begin, _end):
          self.modified = True
          painter.drawPoint(_begin.x(), _end.y())
  
      def pencilOn(self):
          self.set_shape("pencil")
  
  
  class MainWindow(QMainWindow):
      def __init__(self, parent=None):
          super(MainWindow, self).__init__(parent)
          self.scribbleArea = ScribbleArea()
          self.setCentralWidget(self.scribbleArea)
  
          self.pen = QAction(QIcon('Image/pen.png'), 'Карандаш', self)
          self.pen.triggered.connect(self.scribbleArea.pencilOn)
          toolbar = self.addToolBar('Инструменты')
          toolbar.addAction(self.pen)
  
  
  def main():
      app = QApplication(sys.argv)
      window = MainWindow()
      window.show()
      sys.exit(app.exec_())
  
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>The problem is that at the moment a point is drawn that can be moved along the axis. But this is not what I'm waiting for. I assume that instead of _begin.x (), _end.y () need use event.x (), event.y ().</p>
  
  <p>But this does not work out because in the draw function I can not pass the argument to "event".</p>
  
  <p>When I try to do something similar, the function requests event with =</p>
  
  <pre><code>def draw(self, painter, _begin, _end, is_released):
      if _begin.isNull() or _end.isNull():
          return
      elif self.shape() == "pencil":
          self.drawPencil(painter, event= #????)
  
  def drawPencil(self, painter, _begin, _end):
      self.modified = True
      painter.drawPoint(event.x(), event.y())
  </code></pre>
  
  <p>I do not really understand what needs to be passed to the event so that the function works.</p>
  
  <p>There is one more implementation of the pencil function:</p>
  
  <pre><code>def draw_pencil(self, event):
      painter.drawLine(self.lastPoint, event.pos())
      self.modified = True
      self._lastPoint = event.pos()
      self.update()
  </code></pre>