from
PyQt5.QtWidgets
import
*
from
PyQt5.QtGui
import
*
from
PyQt5.QtCore
import
*
import
sys
class
Window(QMainWindow):
def
__init__(
self
):
super
().__init__()
title
=
"Paint and save Application"
top
=
400
left
=
400
width
=
800
height
=
600
self
.setWindowTitle(title)
self
.setGeometry(top, left, width, height)
self
.image
=
QImage(
self
.size(), QImage.Format_RGB32)
self
.image.fill(Qt.white)
mainMenu
=
self
.menuBar()
fileMenu
=
mainMenu.addMenu(
"File"
)
saveAction
=
QAction(
"Save"
,
self
)
saveAction.setShortcut(
"Ctrl + S"
)
fileMenu.addAction(saveAction)
saveAction.triggered.connect(
self
.save)
self
.draw_something()
def
paintEvent(
self
, event):
canvasPainter
=
QPainter(
self
)
canvasPainter.drawImage(
self
.rect(),
self
.image,
self
.image.rect())
def
draw_something(
self
):
painter
=
QPainter(
self
.image)
painter.setPen(QPen(Qt.black,
5
, Qt.SolidLine,
Qt.RoundCap, Qt.RoundJoin))
painter.drawLine(
100
,
100
,
300
,
300
)
self
.update()
def
save(
self
):
filePath, _
=
QFileDialog.getSaveFileName(
self
,
"Save Image"
, "",
"PNG(*.png);;JPEG(*.jpg *.jpeg);;All Files(*.*) "
)
if
filePath
=
=
"":
return
self
.image.save(filePath)
if
__name__
=
=
"__main__"
:
app
=
QApplication(sys.argv)
window
=
Window()
window.show()
sys.exit(app.
exec
())