Skip to content

Instantly share code, notes, and snippets.

@zed

zed/__main__.py Secret

Last active February 8, 2018 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zed/d1b63f81896e091b30b2f83e3ec6c1f1 to your computer and use it in GitHub Desktop.
Save zed/d1b63f81896e091b30b2f83e3ec6c1f1 to your computer and use it in GitHub Desktop.
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