Python GUI How to Create Paint Application in PyQt5

In this Python GUI article, we will learn How to Create Paint Application in PyQt5 that

allows us to draw lines on a canvas with different brush sizes and colors. We will also learn

how to use the QImage class and the mouse events in order to construct the paint program.

also you can watch the video tutorial for this article at the end.

 

 

Also you can check more Python GUI articles in the below links

1: Kivy GUI Development Tutorials

2: Python TKinter GUI Development 

3: Psyide2 GUI Development 

4: wxPython GUI Development 

5: PyQt5 GUI Development Course

 

 

 

 

So now this is the complete code for Python GUI How to Create Paint Application in PyQt5

 

 

 

So now at the top these are the important classes that we are going to use in this simple project.

 

 

 

 

 

And this is our main window class that inherits from QMainWindow, in that class we are going to add the window title , window geometry and minimum width and height for the window. also you can check Python Object Oriented Programming Articles in the link. Python Object Oreinted Programming.  also we are going to create our Menu and Menu Items in this class.

 

 

 

 

OK now in this method we are going to tell PyQt5 what to do when the left mouse button is

pressed .

 

 

 

 

Then, we will create the mouseMoveEvent() event and tell Qt what to do when the mouse is

moving. In this case, we want to draw the lines on the canvas if the left

 

 

 

 

After that, we will also construct the mouseReleaseEvent() event, which will be triggered

when the mouse button is released:

 

 

 

 

Once you’re done with that, we will proceed to the paintEvent() event.

 

 

 

 

Also these are the menu item for the application.

 

 

 

How it Works

In this example, we have created a QImage widget when the program started. This widget acts as the canvas and it will follow the size of the window whenever the window gets resized. In order to draw something on the canvas, we will need to use the mouse events provided by PyQt5. These events will tell us the position of the cursor and we will be able to use this information to change the pixels on the canvas. We use a Boolean variable called drawing to let the program know whether it should start drawing when a mouse button is pressed. In this case, when the left mouse button is pressed, the variable drawing will be set to true. We also save the current cursor position to the lastPoint variable when the left mouse button is pressed, so that PyQt5 will know where it should start drawing. When the mouse moves, the mouseMoveEvent() event will be triggered by PyQt5. This is where we need to check whether the drawing variable is set to true. If it is, then QPainter can start drawing the lines onto the QImage widget based on the brush settings that we provide. The brush settings consist of the brush color as well as the brush size. These settings are being saved as variables and can be altered by selecting a different setting from the menu bar. Please remember to call the update() function when the user is drawing on the canvas. Otherwise, the canvas will remain empty even though we have changed the pixel information of the canvas.

 

 

 

 

 

So now run the complete code and this will be the result.

Python GUI How to Create Paint Application in PyQt5
Python GUI How to Create Paint Application in PyQt5

 

 

 

 

 

Also you can watch the complete video for this article 

 

 

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×