Python Calculator App using tkinter for Windows programming

Posted by


In this tutorial, we will create a simple calculator app using Python and Tkinter, which is a built-in library for creating GUI applications.

Step 1: Set up your environment
First, make sure you have Python installed on your computer. You can download Python from the official website https://www.python.org/. Once Python is installed, you can install Tkinter by running the command pip install tk.

Step 2: Create a new Python file
Open your favorite code editor or IDE and create a new Python file. We will name it calculator_app.py.

Step 3: Import the necessary libraries
In the calculator_app.py file, import the necessary libraries like tkinter for creating the GUI and eval for evaluating mathematical expressions.

from tkinter import *

Step 4: Create the main calculator class
Next, create a Calculator class that will handle the main functionality of the calculator app. In this class, we will define methods like create_widgets to create the GUI elements, process_input to handle user input, and calculate_result to evaluate the mathematical expression.

class Calculator:
    def __init__(self, master):
        self.master = master
        self.master.title("Simple Calculator")
        self.create_widgets()

    def create_widgets(self):
        self.display = Entry(self.master, width=20, borderwidth=5)
        self.display.grid(row=0, column=0, columnspan=4)

        buttons = [
            ('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),
            ('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),
            ('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),
            ('C', 4, 0), ('0', 4, 1), ('=', 4, 2), ('+', 4, 3)
        ]

        for button_text, row, column in buttons:
            Button(self.master, text=button_text, padx=20, pady=15, command=lambda text=button_text: self.process_input(text)).grid(row=row, column=column)

    def process_input(self, text):
        if text == 'C':
            self.display.delete(0, END)
        elif text == '=':
            result = self.calculate_result(self.display.get())
            self.display.delete(0, END)
            self.display.insert(0, str(result))
        else:
            self.display.insert(END, text)

    def calculate_result(self, expression):
        try:
            result = eval(expression)
            return round(result, 2)
        except:
            return 'Error'

Step 5: Initialize the app
Lastly, initialize the calculator app by creating an instance of the Calculator class and running the Tkinter main loop.

if __name__ == '__main__':
    root = Tk()
    app = Calculator(root)
    root.mainloop()

Step 6: Run the app
Save the calculator_app.py file and run it using the command python calculator_app.py. You should see a simple calculator app with buttons for digits, operators, clear, and calculate.

Congratulations! You have successfully created a simple calculator app using Python and Tkinter. Feel free to customize the app by adding more features or styling the GUI elements to make it more visually appealing. Happy coding!

0 0 votes
Article Rating

Leave a Reply

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@MadihaMirza-bc7xb
1 hour ago

how you make front end

@geographyconcepts746
1 hour ago

Very nicely 😅

@XhxhdhDhydjc
1 hour ago

Jiooo bhai❤

@CreativeTechno-k9d
1 hour ago

I love your videos very nice videos …

@Customtkinter
1 hour ago

I saw it 😅

5
0
Would love your thoughts, please comment.x
()
x