≡ Menu

Building Calculator using PyQt6 in Python

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

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 

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.

{ 0 comments… add one }


%d blogger hanno fatto clic su Mi Piace per questo: