Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions paintApplicationPython.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
from PyQt5.QtWidgets import QMainWindow, QApplication, QMenu, QMenuBar, QAction, QFileDialog
from PyQt5.QtGui import QIcon, QImage, QPainter, QPen, QBrush
from PyQt5.QtCore import Qt, QPoint
import sys

class Window(QMainWindow):
def __init__(self):
super().__init__()


title = "Prajjwal's Paint Application"
x = 500
y = 100
width = 1080
height = 720

icon = "icons/Code_Maniac.jpg"

self.setWindowTitle(title)
self.setGeometry(x, y, 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")
brushSize = mainMenu.addMenu("Brush Size")
brushColor = mainMenu.addMenu("Brush Color")
shapes = mainMenu.addMenu("Add Shapes")

circleAction = QAction(QIcon("icons/save.png"), "Cicle", self)
circleAction.setShortcut("Alt+C")
shapes.addAction(circleAction)
circleAction.triggered.connect(self.circle)

rectAction = QAction(QIcon("icons/save.png"), "Rectangle", self)
rectAction.setShortcut("Alt+R")
shapes.addAction(rectAction)
rectAction.triggered.connect(self.rectangle)


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("3px", self)
brushSize.addAction(threepxAction)
threepxAction.triggered.connect(self.threePixel)

fivepxAction = QAction("5px", self)
brushSize.addAction(fivepxAction)
fivepxAction.triggered.connect(self.fivePixel)

sevenpxAction = QAction("7px", self)
brushSize.addAction(sevenpxAction)
sevenpxAction.triggered.connect(self.sevenPixel)

ninepxAction = QAction("9px", self)
brushSize.addAction(ninepxAction)
ninepxAction.triggered.connect(self.ninePixel)

blackAction = QAction(QIcon("icons/black.png"), "Black", self)
blackAction.setShortcut("Ctrl+B")
brushColor.addAction(blackAction)
blackAction.triggered.connect(self.blackColor)


whitekAction = QAction(QIcon("icons/white.png"), "White", self)
whitekAction.setShortcut("Ctrl+W")
brushColor.addAction(whitekAction)
whitekAction.triggered.connect(self.whiteColor)


redAction = QAction(QIcon("icons/red.png"), "Red", self)
redAction.setShortcut("Ctrl+R")
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.greenColor)

yellowAction = QAction(QIcon("icons/yellow.png"), "Yellow", self)
yellowAction.setShortcut("Ctrl+Y")
brushColor.addAction(yellowAction)
yellowAction.triggered.connect(self.yellowColor)

def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = True
self.lastPoint = event.pos()
#print(self.lastPoint)


def exit_paint(self, event):
if event.type() == KEY_DOWN:
if event.key() == Qt.Key_Escape:
self.close()

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 threePixel(self):
self.brushSize = 3

def fivePixel(self):
self.brushSize = 5

def sevenPixel(self):
self.brushSize = 7

def ninePixel(self):
self.brushSize = 9


def blackColor(self):
self.brushColor = Qt.black

def whiteColor(self):
self.brushColor = Qt.white

def redColor(self):
self.brushColor = Qt.red

def greenColor(self):
self.brushColor = Qt.green

def yellowColor(self):
self.brushColor = Qt.yellow


def circle(self):
painter = QPainter(self)
painter.setPen(QPen(Qt.green, 5, Qt.SolidLine))
painter.setBrush(QBrush(Qt.green, Qt.SolidPattern))
painter.drawEllipse(40, 40, 400, 200)
pass


def rectangle(self):
pass

if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
app.exec()