Clearing the canvas in QPainter

My canvas is not clearing every time I run the app. I tried adding update() to the code but it doesn’t seem to work. Please can you help me figure out what I am doing wrong? Your previous tutorials have all been great, but this one is lacking a little bit of detail.

When you say it’s not clearing, what do you mean exactly? Is it appearing black maybe? It could be that you need to fill the canvas with white to begin with, can you try add the following canvas fill line?

canvas = QtGui.QPixmap(400, 300)
canvas.fill(QtGui.QColor('#ffffff')) # Fill entire canvas.

(Also, which version of PyQt5 are you using, and which platform?)

Thank you for your reply. I figured out that if I add self.update() after painter.end() in all my functions the canvas does clear. I’m using the most recent version of pyqt5 on Windows 10 with Spyder.

Since it’s Windows I also have a black background, so I’ve been changing the pen colour. I really appreciate the code to change the background - I searched all over the web for the code to do that but couldn’t find anything relevant to pyqt5 and the background in this particular situation. Now it looks like I don’t need to call self.update() if I have the background filled.