Simple GUI calculator using PyQt5 in Python

A calculator is something used for performing mathematical calculations, particularly a compact electronic device having a keyboard and a visual display. In this post, we'll look at how to make one using PyQt5.

Steps for implementing a GUI :

  • Create a label, specify its shape and display the numbers and output on it.
  • Increase the font size and align the label text from the right side.
  • Make push buttons for the numerals 0 through 9 and arrange their geometries in the correct sequence.
  • Make an example of an operator push button for addition, subtraction, etc.
  • To draw attention to the equals button, give it a colour effect.

Back-end implementation procedure :

  • Give each button a function.
  • Add the label's text with the appropriate number or operator within each button's actions, with the exception of the equals to action.
  • Get the label's text from within the equals to action and begin the try except block.
  • Use the eval method on the label text inside the try block to obtain the response and set the answer to the label.
  • Set the text "Wrong Input" within the unless block.
  • Make the label's final character disappear for the delete action, and leave the entire text blank for the clear action.

Understanding the code step by step :

First of all, we will import all the modules needed by our program like the PyQt5 and sys modules.

Then, we will set the title and dimensions of the window and will call the UI components function to display all the widgets.

Then, we call the UI components function for creating an element label and setting the stylesheet for Labels including the alignment and fonts.

Then, we will create push buttons for all the numbers from 0-9 and set their geometry and size.

Then, we will create push buttons for all the mathematical operators and clear, equal & delete keywords along with setting their geometry and size. The equal button was given special emphasis by adding different colour effect to it.

Then, we added actions to all these push buttons by attaching event calling functions.

Then, the event calling functions that that were added to each push button were defined. The functions were created for all the number from 0-9. These functions on being called proceeded in such a way that if some text is present then that text would be added/ subtracted/ multiplied/ divided/ equated by the next number pressed. And, till the equals to operator button was not pressed, all the results are stored temporarily in a variable, which is displayed after getting the final answer.

The clear button's corresponding function is used to clear all the text in the text area and the delete button's corresponding function is used to delete only one current character in the text area.

Finally, we will create the PyQt5 application by creating an instance of the window and then executed the application's code.

Complete Code :

Output:

Simple GUI calculator using PyQt5 in Python
Simple GUI calculator using PyQt5 in Python
Simple GUI calculator using PyQt5 in Python




Latest Courses