Skip to content

Instantly share code, notes, and snippets.

@Gnumaru
Created September 14, 2019 18:10
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 Gnumaru/e1e88d146334aac84ab6530c150928f1 to your computer and use it in GitHub Desktop.
Save Gnumaru/e1e88d146334aac84ab6530c150928f1 to your computer and use it in GitHub Desktop.
transcription for the code found in the video "PyQt5 Creating Paint Application In 40 Minutes" https://www.youtube.com/watch?v=qEgyGyVA1ZQ
# transcription for the code found in the video "PyQt5 Creating Paint Application In 40 Minutes" https://www.youtube.com/watch?v=qEgyGyVA1ZQ
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu, QAction, QFileDialog
from PyQt5.QtGui import QIcon, QImage, QPainter, QPen
from PyQt5.QtCore import Qt, QPoint
import sys
class Window (QMainWindow) :
def __init__(self):
super().__init__()
top = 400
left = 400
width = 800
height = 600
icon = "icons/pain.png"
self.setWindowTitle ("Paint Application")
self.setGeometry(top, left, width, height)
self.setWindowIcon(QIcon (icon))
self.image = QImage (self.size(), QImage.Format_RGB32)
self.image.fill(Qt.white)
self.drawing = False
self.brushSize = 2
self.brushColor = Qt.black
self.lastPoint = QPoint ()
mainMenu = self.menuBar ()
fileMenu = mainMenu.addMenu("File")
brushMenu = mainMenu.addMenu ("Brush Size")
brushColor = mainMenu.addMenu ("Brush Color")
saveAction = QAction (QIcon ("icons/save.png"), "Save", self)
saveAction.setShortcut ("Ctrl+S")
fileMenu.addAction (saveAction)
saveAction.triggered.connect (self.save)
clearAction = QAction (QIcon ("icons/clear.png"), "Clear", self)
clearAction.setShortcut ("Ctrl+C")
fileMenu.addAction (clearAction)
clearAction.triggered.connect (self.clear)
threepxAction = QAction (QIcon ("icons/threepx.png"), "3px", self)
threepxAction.setShortcut ("Ctrl+T")
brushMenu.addAction (threepxAction)
threepxAction.triggered.connect (self.threePx)
fivepxAction = QAction (QIcon ("icons/fivepx.png"), "5px", self)
fivepxAction.setShortcut ("Ctrl+F")
brushMenu.addAction (fivepxAction)
fivepxAction.triggered.connect (self. fivePx)
sevenpxAction = QAction (QIcon ("icons/sevenpx.png"), "7px", self)
sevenpxAction.setShortcut ("Ctrl+S")
brushMenu.addAction (sevenpxAction)
sevenpxAction.triggered.connect (self.sevenPx)
ninepxAction = QAction (QIcon ("icons/ninepx.png"), "9px", self)
ninepxAction.setShortcut ("Ctrl+N")
brushMenu.addAction (ninepxAction)
ninepxAction.triggered.connect (self.ninePx)
blackAction = QAction (QIcon ("icons/black.png"), "Black", self)
blackAction.setShortcut ("Ctrl+B")
brushColor.addAction (blackAction)
blackAction.triggered.connect (self.blackColor)
redAction = QAction (QIcon ("icons/red.png"), "Red", self)
redAction.setShortcut ("Ctrl+W")
brushColor.addAction (redAction)
redAction. triggered.connect (self.redColor)
greenAction = QAction (QIcon ("icons/green.png"), "Green", self)
greenAction.setShortcut ("Ctrl+G")
brushColor.addAction (greenAction)
greenAction.triggered.connect (self.greenkColor)
yellowAction = QAction (QIcon ("icons/yellow.png"), "Yellow", self)
yellowAction.setShortcut ("Ctrl+G")
brushColor.addAction (yellowAction)
yellowAction.triggered.connect (self.yellowColor)
def mousePressEvent (self, event):
if event.button () == Qt.LeftButton:
self.drawing = True
self.lastPoint = event.pos ()
def mouseMoveEvent(self, event):
if (event.buttons () & Qt.LeftButton) & self.drawing:
painter = QPainter (self.image)
painter.setPen(QPen(self.brushColor, self.brushSize, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
painter.drawLine(self.lastPoint, event.pos())
self.lastPoint = event.pos ()
self.update()
def mouseReleaseEvent (self, event):
if event.button == Qt.LeftButton:
self.drawing = False
def paintEvent (self, event):
canvasPainter = QPainter (self)
canvasPainter.drawImage (self.rect (), self.image, self.image.rect ( ) )
def save (self):
filePath, _= QFileDialog.getSaveFileName (self, "Save Image", "", "PNG(*.png);;JPEG(*.jpg *.jpeg);; ALL Files(*.*)")
if filePath == "":
return
self.image.save (filePath)
def clear (self):
self.image.fill(Qt.white)
self.update()
def threePx (self):
self.brushSize = 3
def fivePx (self):
self.brushSize = 5
def sevenPx (self):
self.brushSize = 7
def ninePx (self):
self.brushSize = 9
def blackColor (self):
self.brushColor = Qt.black
def redColor (self):
self.brushColor = Qt.red
def greenkColor (self):
self.brushColor = Qt.green
def yellowColor(self):
self.brushColor = Qt.yellow
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
app.exec()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment