-
-
Save zed/d1b63f81896e091b30b2f83e3ec6c1f1 to your computer and use it in GitHub Desktop.
pyqt5 draw on mouse press
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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