Now write specific code is as follows, hope you guys help me answer the,
The from PyQt5. QtCore import *
The from PyQt5. QtGui import *
The from PyQt5. QtWidgets import *
The class PaintBoard (QWidget) :
Def __init__ (self, parent=None) :
Super (PaintBoard, self) __init__ (parent)
Self. InitData ()
Self. InitView ()
Def initData (self) :
Self. Size=QSize (600, 900)
Self. Board=QPixmap (self. The size)
The self. The board. The fill (Qt. White) # sketchpad filling to white
Self. IsEmpty=True # initial default empty sketchpad
Self. LastPoint=QPoint (0, 0)
The self. The endPoint=QPoint (0, 0)
# auxiliary canvas
Self. TempBoard=QPixmap ()
Are self. IsDrawing=False # symbol drawing
Def initView (self) :
Pass
Def paintEvent (self, event) :
Painter=QPainter (self)
X=self. LastPoint. (x)
Y=self. LastPoint. (y)
W=self. The endPoint. (x) - x
H=the self. The endPoint. (y) - y
If self. IsDrawing:
Self. TempBoard=self. Board
Pen=QPainter (self. TempBoard)
Pen. DrawRect (x, y, w, h)
Painter. DrawPixmap (0, 0, the self. TempBoard)
The else:
Pen=QPainter (self. Board)
Pen. DrawRect (x, y, w, h)
Painter. DrawPixmap (0, 0, the self. Board)
Def mousePressEvent (self, event) :
# by pressing the left mouse button
If the event. The button ()==Qt. LeftButton:
The self. The lastPoint=event. The pos ()
The self. The endPoint=self. LastPoint
Self. IsDrawing=True
Def mouseReleaseEvent (self, event) :
# release the left mouse button
If the event. The button ()==Qt. LeftButton:
The self. The endPoint=event. Pos ()
# redraw
The self. The update ()
Self. IsDrawing=False
Def mouseMoveEvent (self, event) :
If the event. The buttons () and Qt. LeftButton:
The self. The endPoint=event. Pos ()
The self. The update ()