Skip to content

Instantly share code, notes, and snippets.

@zed zed/ Secret
Last active Feb 8, 2018

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):
self.point = Qt.QPoint() # the current point to draw
def mousePressEvent(self, event):
self.point = event.pos() # set point to the current mouse coordinates
def paintEvent(self, _):
qp = Qt.QPainter(self)
qp.setPen(Qt.QPen(, 10))
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())
return app.exec_()
if __name__ == '__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.