In this article we will see how we can create a calculator using PyQt6.
A calculator is something used for making mathematical calculations, in particular a small electronic device with a keyboard and a visual display. Below is the how the calculator will looks like
![](https://i0.wp.com/www.raucci.net/wp-content/uploads/2021/11/Schermata-2021-11-06-alle-19.10.48-1.jpg?resize=321%2C415)
GUI implementation steps
- Create a label to show the numbers and the output and set its geometry
- Align the label text fro right side and increase the font size of it
- Create push buttons for the numbers from 0 to 9 and set their geometry in the proper order
- Create operator push button example for addition, subtraction etc
![](https://i0.wp.com/www.raucci.net/wp-content/uploads/2021/11/Schermata-2021-11-06-alle-19.13.32.jpg?resize=1024%2C517)
Back end implementation steps
- Add action to each button
- Inside the actions of each button except the equals to action, append the text of the label with the respective number or the operator
- Inside the equals to action get the text of the label and start the try except block
- Inside the try block use eval method on the label text to get the ans and set the answer to the label
- Inside the except block set “ERROR” as text
- For delete action make the last character removed from the label and for the clear action set the whole text to blank.
Here, you can download the code sources.