我正在尝试设计一个我可以在其中绘制的简单程序。我想在点击鼠标时在屏幕上绘制点。我正在尝试使用QPainter,但它似乎没有按预期工作(也就是说,当我点击时,屏幕上没有任何内容)。任何帮助将不胜感激。
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import *
import sys, random
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.qp = QPainter()
self.qp.begin(self)
self.setGeometry(300, 300, 300, 190)
self.setWindowTitle('Draw')
self.y = 0
self.x = 0
self.show()
def drawPoints(self, qp):
qp.setPen(Qt.red)
size = self.size()
qp.drawPoint(self.x, self.y)
def mouseMoveEvent(self, QMouseEvent):
pos = QMouseEvent.pos()
self.x = pos.x()
self.y = pos.y()
self.drawPoints(self.qp)
def closeEvent(self, QCloseEvent):
self.qp.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())