There are so many options provided by Python to develop GUI application and PyQt5 is one of them. PyQt5 is cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided by this library.
In this article we will see how we can create a Paint application using PyQt5. Our Paint application will consist of following Features :
- User can select different brush sizes
- User can select different brush color
- Saving of the canvas
- Clearing the whole canvas at once
Steps for designing the widgets –
1. Create a window sets its geometry and title
2. Create menu bar
3. Inside menu bar add different menus, that are file menu, size menu and color menu
4. Add save and clear action to the file menu
5. Add different brush sizes action to the brush size menu
6. Add different brush color action to the brush color menu
7. Create a white canvas and add it to the window
Back-end steps :
1. Create different variable : Drawing flag to check if currently drawing or not and set it to False, brush size variable to set current brush size, brush color to set current brush color and current position variable to know position of cursor
2. Add action to the clear and save widget
3. Inside clear action fill the canvas with white color
4. Inside the save action save the canvas
5. Add actions(methods) to various brush sizes and color to set size and color
6. Create paint event to draw white canvas on the screen
7. Create method to know when mouse left button is pressed inside that method make drawing flag true and change the current position
8. Create method for mouse movement, inside this method check if drawing flag is true and button is still pressed then draw using painter object and change the position according.
9. Create a method to know mouse button is released inside this method make drawing flag to false.
Below is the implementation
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.