Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 08af154

Browse files
paint script added
1 parent 08fbdec commit 08af154

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed

‎Paint Application/paint.py‎

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# importing libraries
2+
from PyQt5.QtWidgets import *
3+
from PyQt5.QtGui import *
4+
from PyQt5.QtCore import *
5+
import sys
6+
7+
# window class
8+
class Window(QMainWindow):
9+
def __init__(self):
10+
super().__init__()
11+
12+
# setting application window up
13+
self.setWindowTitle("Paint with PyQt5")
14+
self.setGeometry(100, 100, 800, 600)
15+
16+
# setting up image object
17+
self.image = QImage(self.size(), QImage.Format_RGB32)
18+
self.image.fill(Qt.white)
19+
20+
# variables
21+
# drawing flag - shows if currently drawing
22+
self.drawing = False
23+
self.brushSize = 2
24+
self.brushColor = Qt.black
25+
# QPoint object to track the point of cursor release
26+
self.lastPoint = QPoint()
27+
28+
# creating menu bar
29+
mainMenu = self.menuBar()
30+
fileMenu = mainMenu.addMenu("File")
31+
b_size = mainMenu.addMenu("Brush Size")
32+
b_color = mainMenu.addMenu("Brush Color")
33+
34+
# creating save action
35+
saveAction = QAction("Save", self)
36+
saveAction.setShortcut("Ctrl + S")
37+
fileMenu.addAction(saveAction)
38+
saveAction.triggered.connect(self.save)
39+
40+
# creating clear action
41+
clearAction = QAction("Clear", self)
42+
clearAction.setShortcut("Ctrl + C")
43+
fileMenu.addAction(clearAction)
44+
clearAction.triggered.connect(self.clear)
45+
46+
# creating options for brush sizes
47+
pix_4 = QAction("4px", self)
48+
b_size.addAction(pix_4)
49+
pix_4.triggered.connect(self.Pixel_4)
50+
51+
pix_7 = QAction("7px", self)
52+
b_size.addAction(pix_7)
53+
pix_7.triggered.connect(self.Pixel_7)
54+
55+
pix_9 = QAction("9px", self)
56+
b_size.addAction(pix_9)
57+
pix_9.triggered.connect(self.Pixel_9)
58+
59+
pix_12 = QAction("12px", self)
60+
b_size.addAction(pix_12)
61+
pix_12.triggered.connect(self.Pixel_12)
62+
63+
# creating options for brush color
64+
black = QAction("Black", self)
65+
b_color.addAction(black)
66+
black.triggered.connect(self.blackColor)
67+
68+
white = QAction("White", self)
69+
b_color.addAction(white)
70+
white.triggered.connect(self.whiteColor)
71+
72+
green = QAction("Green", self)
73+
b_color.addAction(green)
74+
green.triggered.connect(self.greenColor)
75+
76+
yellow = QAction("Yellow", self)
77+
b_color.addAction(yellow)
78+
yellow.triggered.connect(self.yellowColor)
79+
80+
red = QAction("Red", self)
81+
b_color.addAction(red)
82+
red.triggered.connect(self.redColor)
83+
84+
85+
# method for checking mouse clicks
86+
def mousePressEvent(self, event):
87+
88+
# if left mouse button is pressed
89+
if event.button() == Qt.LeftButton:
90+
# set drawing flag true
91+
self.drawing = True
92+
# set last point to the point of cursor
93+
self.lastPoint = event.pos()
94+
95+
# method for tracking mouse activity (enables drawing)
96+
def mouseMoveEvent(self, event):
97+
98+
# checking if left button is pressed and drawing flag is true
99+
if (event.buttons() & Qt.LeftButton) & self.drawing:
100+
101+
# creating painter object
102+
painter = QPainter(self.image)
103+
# set the pen of the painter
104+
painter.setPen(QPen(self.brushColor, self.brushSize,
105+
Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
106+
# draw line from the last point of cursor to the current point
107+
painter.drawLine(self.lastPoint, event.pos())
108+
self.lastPoint = event.pos()
109+
self.update()
110+
111+
# method for mouse left button release
112+
def mouseReleaseEvent(self, event):
113+
114+
if event.button() == Qt.LeftButton:
115+
# make drawing flag false
116+
self.drawing = False
117+
118+
# paint event
119+
def paintEvent(self, event):
120+
# create a canvas
121+
canvasPainter = QPainter(self)
122+
canvasPainter.drawImage(self.rect(), self.image, self.image.rect())
123+
124+
# method for saving canvas
125+
def save(self):
126+
filePath, _ = QFileDialog.getSaveFileName(self, "Save Image", "",
127+
"PNG(*.png);;JPEG(*.jpg *.jpeg);;All Files(*.*) ")
128+
if filePath == "":
129+
return
130+
self.image.save(filePath)
131+
132+
# method for clearing canvas
133+
def clear(self):
134+
# make the whole canvas white
135+
self.image.fill(Qt.white)
136+
self.update()
137+
138+
# methods for changing pixel sizes
139+
def Pixel_4(self):
140+
self.brushSize = 4
141+
142+
def Pixel_7(self):
143+
self.brushSize = 7
144+
145+
def Pixel_9(self):
146+
self.brushSize = 9
147+
148+
def Pixel_12(self):
149+
self.brushSize = 12
150+
151+
# methods for changing brush color
152+
def blackColor(self):
153+
self.brushColor = Qt.black
154+
155+
def whiteColor(self):
156+
self.brushColor = Qt.white
157+
158+
def greenColor(self):
159+
self.brushColor = Qt.green
160+
161+
def yellowColor(self):
162+
self.brushColor = Qt.yellow
163+
164+
def redColor(self):
165+
self.brushColor = Qt.red
166+
167+
168+
169+
# create pyqt5 app
170+
App = QApplication(sys.argv)
171+
172+
# create the instance of our Window
173+
window = Window()
174+
window.show()
175+
176+
# start the app
177+
sys.exit(App.exec())

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /