Skip to content

Instantly share code, notes, and snippets.

@zed zed/__main__.py Secret
Last active Feb 8, 2018

Embed
What would you like to do?
pyqt5 draw on mouse press
#!/usr/bin/env python3
import sys
from PyQt5 import Qt, QtCore
class Canvas(Qt.QWidget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.point = Qt.QPoint() # the current point to draw
def mousePressEvent(self, event):
self.point = event.pos() # set point to the current mouse coordinates
self.update()
def paintEvent(self, _):
qp = Qt.QPainter(self)
qp.setPen(Qt.QPen(QtCore.Qt.black, 10))
qp.drawPoint(self.point)
def main():
app = Qt.QApplication(sys.argv)
w = Canvas()
w.setWindowTitle('Draw point on mouse press')
w.resize(400, 300)
# center
w.adjustSize() # update w.rect() now
w.move(app.desktop().screen().rect().center() - w.rect().center())
w.show()
return app.exec_()
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.