I need the picture drawn when the key is pressed to shift and change. I use QPainter, swears that

QPainter :: drawPoints: Painter not active.

So it goes. Help the unfortunate student :) What is the best way to use for dynamic picture rendering? Or maybe I do not know any spells?

def paintEvent(self, e): self.sk = QtGui.QPainter() self.sk.begin() self.drawBack() self.drawStars() self.sk.end() def drawBack(self): white_pen = QtGui.QPen(QtGui.QColor('white'), 3) self.sk.setPen(white_pen) self.sk.setBrush(QtGui.QColor('midnightblue')) self.sk.drawRect(0, 0, self.width(), self.height()) def drawStars(self): self.sk.setPen(QtGui.QColor('white')) size = self.size() if self.stars: for a in self.stars: self.sk.drawPoint(ax - 1, ay) else: for i in range(10): x = random.randint(1, size.width()-1) y = random.randint(1, size.height()-1) self.stars.add(StarAndDec(x, y)) self.sk.drawPoint(x, y) def keyPressEvent(self, event): key = event.key() if key == QtCore.Qt.Key_Left: for a in self.stars: ax = ax - 1 self.drawStars()` 

    1 answer 1

    Drawing in QPainter can only be used in its paintEvent ; place the drawing code in it. In keyPressEvent place the line sk.update() to trigger that event.