How to Build Calculator in Python TKinter

In this article we want to learn How to Build Calculator in Python TKinter, so first of all let’s talk about TKinter, if you are Python Developer, you know that when it comes to building GUI applications in Python, you have different choices one of them are TKinter, now What is Python TKinter?

 

 

 

What is TKinter?

Tkinter is Python library that helps you create graphical user interfaces (GUIs) for your programs. It provides different components, like buttons, labels and entry fields, that you can use to build windows, menus and other visual elements for your applications. TKinter is already packed with Python, so you don’t need to install that.

 

 

Now to Build Calculator in Python TKinter you need to follow these steps

 

 

1: First you need to import required libraries

 

2: After that you need to create a class that extends from tk.Tk

 

 

3: Then you need to spcifiy the title and geometry of the window

 

 

4: In here we creates an Entry widget, which is essentially a single-line text entry field where users can input text or numbers. also we arranges the Entry widget within its parent widget using the grid geometry manager. 

 

 

5: These are the button list that we want to create

 

 

6: This code creates a grid of buttons in a Tkinter window.

  • For each button in the button_list, it creates a Tkinter Button widget with the specified text, width, height, and a command to execute when clicked.
  • The button is placed in the grid layout with the specified row and column indices, along with padding on the x and y axes.
  • After placing each button, the column index  (col) is incremented. If it exceeds 3 (indicating the fourth column in a zero-based index), it resets to 0 and the row index (row) is incremented to move to the next row.
  • Finally, a separate button for “=” is created and placed in the fifth row and fourth column of the grid.

 

 

 

7: This method is called when any button in the calculator is clicked, and it handles the input accordingly.

  • If the clicked button is “C” (clear), it deletes all text from the Entry widget (self.result) where users input numbers and operators.
  • Otherwise, it inserts the text of the clicked button at the end of the current content in the Entry widget. For example, if the user clicks the button “7”, the digit “7” will be added to the end of the existing content.

 

 

 

8: This method performs the calculation based on the input expression entered in the calculator.

  • It retrieves the input expression from the Entry widget (self.result) where users input numbers and operators.
  • Using a regular expression(re.sub(r'[^0-9\+\-\*\/\.]’, ”, self.result.get())), it removes any characters from the input expression that are not digits, arithmetic operators (+, -, *, /), or decimal points.
  • Then, it attempts to evaluate the sanitized input expression using Python eval() function, which calculates the result of the expression.
  • If the calculation succeeds, the result is displayed in the Entry widget.
  • If there’s an error during the calculation (such as division by zero or invalid input), it displays “Error” in the Entry widget.

 

 

 

 

This is the complete code for building calculator with Python TKinter

 

 

 

Run the complete code and this will be the result

How to Build Calculator in Python TKinter
How to Build Calculator in Python TKinter

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×