Building a Painting Application with PyQt in Python, Including File Saving! | PyQt6 Tutorial Part 9

Posted by


In this tutorial, we will be creating a painting app using PyQt in Python. This app will allow users to draw on a canvas using different colors and brush sizes. Additionally, we will add the functionality to save the drawing to a file so that users can save their work for later.

Step 1: Set up the project

First, make sure you have PyQt6 installed in your Python environment. You can install it using pip:

pip install PyQt6

Next, create a new Python script for our painting app. Let’s name it painting_app.py.

Step 2: Import necessary libraries

In the Python script, import the necessary libraries:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget, QPushButton, QColorDialog, QHBoxLayout, QFileDialog
from PyQt6.QtGui import QPixmap, QPainter, QColor, QPen, QImage
from PyQt6.QtCore import Qt, QSize

Step 3: Create the main window class

Create a class for the main window of our painting app:

class PaintingApp(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Painting App")
        self.canvas = Canvas()
        self.setCentralWidget(self.canvas)
        self.create_toolbar()

    def create_toolbar(self):
        toolbar = QWidget()
        self.setMenuWidget(toolbar)

        layout = QVBoxLayout(toolbar)
        toolbar.setLayout(layout)

        color_button = QPushButton("Select Color", clicked=self.select_color)
        layout.addWidget(color_button)

        save_button = QPushButton("Save", clicked=self.save_image)
        layout.addWidget(save_button)

    def select_color(self):
        color = QColorDialog.getColor()
        self.canvas.set_color(color)

    def save_image(self):
        file_path, _ = QFileDialog.getSaveFileName(self, "Save Image", "", "PNG Files (*.png)")
        self.canvas.save_image(file_path)

Step 4: Create the canvas class

Create a class for the canvas where users can draw:

class Canvas(QWidget):
    def __init__(self):
        super().__init__()
        self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
        self.set_color(Qt.GlobalColor.black)
        self.brush_size = 5
        self.last_point = None
        self.image = QImage(self.size(), QImage.Format_ARGB32)
        self.image.fill(Qt.GlobalColor.white)

    def set_color(self, color):
        self.color = color

    def save_image(self, file_path):
        self.image.save(file_path)

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawImage(0, 0, self.image)

    def mousePressEvent(self, event):
        self.last_point = event.position()

    def mouseMoveEvent(self, event):
        if self.last_point is None:
            return

        painter = QPainter(self.image)
        painter.setPen(QPen(self.color, self.brush_size, Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap, Qt.PenJoinStyle.RoundJoin))
        painter.drawLine(self.last_point, event.position())
        self.last_point = event.position()
        self.update()

Step 5: Run the app

Finally, add the following code at the end of the script to run the app:

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = PaintingApp()
    window.show()
    sys.exit(app.exec())

Now, you can run the script and start drawing on the canvas. You can select different colors and brush sizes using the toolbar. You can also save your drawing to a file by clicking the "Save" button.

Congratulations! You have successfully created a painting app with PyQt in Python that allows users to save their drawings to a file. Feel free to customize the app further by adding additional features and functionality. Happy coding!

0 0 votes
Article Rating

Leave a Reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@jack_yun
2 hours ago

hi! quick question: im trying to make it into a multi server connection, like when multiple people can draw on it on the same time, do you have ideas on how to do it?

1
0
Would love your thoughts, please comment.x
()
x