Skip to content

Commit 16f6d23

Browse files
committed
Changes as in #6 Python GUI Desktop Application
1 parent 728b0cb commit 16f6d23

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

Paint Application/paint.py

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
from PyQt5.QtWidgets import *
2+
from PyQt5.QtGui import *
3+
from PyQt5.QtCore import *
4+
import sys
5+
6+
class Window(QMainWindow):
7+
def __init__(self):
8+
super().__init__()
9+
10+
self.setWindowTitle("Paint with PyQt5")
11+
12+
self.setGeometry(100, 100, 800, 600)
13+
14+
self.image = QImage(self.size(), QImage.Format_RGB32)
15+
16+
self.image.fill(Qt.white)
17+
18+
self.drawing = False
19+
self.brushSize = 2
20+
self.brushColor = Qt.black
21+
22+
self.lastPoint = QPoint()
23+
24+
mainMenu = self.menuBar()
25+
26+
fileMenu = mainMenu.addMenu("File")
27+
28+
b_size = mainMenu.addMenu("Brush Size")
29+
30+
b_color = mainMenu.addMenu("Brush Color")
31+
32+
saveAction = QAction("Save", self)
33+
saveAction.setShortcut("Ctrl + S")
34+
fileMenu.addAction(saveAction)
35+
saveAction.triggered.connect(self.save)
36+
37+
clearAction = QAction("Clear", self)
38+
clearAction.setShortcut("Ctrl + C")
39+
fileMenu.addAction(clearAction)
40+
clearAction.triggered.connect(self.clear)
41+
42+
pix_4 = QAction("4px", self)
43+
b_size.addAction(pix_4)
44+
pix_4.triggered.connect(self.Pixel_4)
45+
46+
pix_7 = QAction("7px", self)
47+
b_size.addAction(pix_7)
48+
pix_7.triggered.connect(self.Pixel_7)
49+
50+
pix_9 = QAction("9px", self)
51+
b_size.addAction(pix_9)
52+
pix_9.triggered.connect(self.Pixel_9)
53+
54+
pix_12 = QAction("12px", self)
55+
b_size.addAction(pix_12)
56+
pix_12.triggered.connect(self.Pixel_12)
57+
58+
black = QAction("Black", self)
59+
b_color.addAction(black)
60+
black.triggered.connect(self.blackColor)
61+
62+
white = QAction("White", self)
63+
b_color.addAction(white)
64+
white.triggered.connect(self.whiteColor)
65+
66+
green = QAction("Green", self)
67+
b_color.addAction(green)
68+
green.triggered.connect(self.greenColor)
69+
70+
yellow = QAction("Yellow", self)
71+
b_color.addAction(yellow)
72+
yellow.triggered.connect(self.yellowColor)
73+
74+
red = QAction("Red", self)
75+
b_color.addAction(red)
76+
red.triggered.connect(self.redColor)
77+
78+
79+
def mousePressEvent(self, event):
80+
81+
if event.button() == Qt.LeftButton:
82+
self.drawing = True
83+
self.lastPoint = event.pos()
84+
85+
def mouseMoveEvent(self, event):
86+
87+
if (event.buttons() & Qt.LeftButton) & self.drawing:
88+
89+
painter = QPainter(self.image)
90+
91+
painter.setPen(QPen(self.brushColor, self.brushSize,
92+
Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
93+
94+
painter.drawLine(self.lastPoint, event.pos())
95+
96+
self.lastPoint = event.pos()
97+
self.update()
98+
99+
def mouseReleaseEvent(self, event):
100+
101+
if event.button() == Qt.LeftButton:
102+
self.drawing = False
103+
104+
def paintEvent(self, event):
105+
canvasPainter = QPainter(self)
106+
107+
canvasPainter.drawImage(self.rect(), self.image, self.image.rect())
108+
109+
def save(self):
110+
filePath, _ = QFileDialog.getSaveFileName(self, "Save Image", "",
111+
"PNG(*.png);;JPEG(*.jpg *.jpeg);;All Files(*.*) ")
112+
113+
if filePath == "":
114+
return
115+
self.image.save(filePath)
116+
117+
def clear(self):
118+
self.image.fill(Qt.white)
119+
self.update()
120+
121+
def Pixel_4(self):
122+
self.brushSize = 4
123+
124+
def Pixel_7(self):
125+
self.brushSize = 7
126+
127+
def Pixel_9(self):
128+
self.brushSize = 9
129+
130+
def Pixel_12(self):
131+
self.brushSize = 12
132+
133+
def blackColor(self):
134+
self.brushColor = Qt.black
135+
136+
def whiteColor(self):
137+
self.brushColor = Qt.white
138+
139+
def greenColor(self):
140+
self.brushColor = Qt.green
141+
142+
def yellowColor(self):
143+
self.brushColor = Qt.yellow
144+
145+
def redColor(self):
146+
self.brushColor = Qt.red
147+
148+
149+
150+
App = QApplication(sys.argv)
151+
152+
window = Window()
153+
154+
window.show()
155+
156+
sys.exit(App.exec())

0 commit comments

Comments
 (0)