Python Calculator with GUI: Step-by-Step Guide
Python is one of the most popular programming languages, and it’s not hard to see why. With its simple syntax and powerful libraries, you can build almost anything. One such project is creating a calculator with a graphical user interface (GUI). If you’re looking to build a Python calculator with GUI, you’ve come to the right place! In this article, we’ll walk you through the steps to create a fully functional calculator using Python and Tkinter, a standard GUI library.
1. What is a Python Calculator with GUI?
A Python calculator with a GUI is an application that allows users to perform mathematical calculations through a visual interface. Unlike traditional calculators that use only buttons and screens, a GUI calculator has buttons for numbers and operations that users can click on to perform tasks. The interface is designed in such a way that it’s easy for users to input numbers and see the results on the screen.
For creating such an application, we will use the Tkinter library, which is built into Python and allows for creating GUI applications easily. Tkinter is a great choice for building simple applications like this one because of its simplicity and ease of use.
2. Prerequisites for Building a Python Calculator with GUI
Before you begin building the Python calculator with GUI, there are a few things you need to have in place:
- Python Installed: Make sure you have Python installed on your computer. You can download it from the official Python website.
- Tkinter Library: Tkinter comes bundled with Python, so you don’t need to install anything extra. If for some reason it’s not installed, you can install it manually using
pip install tkinter. - Basic Knowledge of Python: It helps if you’re already familiar with the basics of Python programming, as we’ll be using functions, loops, and event handling in this project.
3. Setting Up the Project
Let’s get started! First, you’ll need to set up a new Python file. You can name it something like calculator.py or any other name that makes sense to you. Once that’s done, let’s start by importing the Tkinter library.
import tkinter as tk
Now that we have Tkinter ready to go, let’s set up the main window for our calculator. This is where all the buttons and display will appear. You can use the following code to create the window:
root = tk.Tk()
root.title("Python Calculator")
root.geometry("400x500") # Adjust the window size
Here, we create a main window using the Tk() function. We also set the title of the window to "Python Calculator" and adjust the size of the window with the geometry() function.
4. Adding Buttons for Numbers and Operations
The next step is to add buttons to the GUI for numbers (0-9) and mathematical operations (addition, subtraction, multiplication, and division). Tkinter provides a Button widget that we can use to create each button.
Here’s how you can add buttons for the numbers 0-9 and the basic mathematical operations:
# Create buttons for numbers
button_1 = tk.Button(root, text="1", width=10, height=3, command=lambda: press(1))
button_2 = tk.Button(root, text="2", width=10, height=3, command=lambda: press(2))
button_3 = tk.Button(root, text="3", width=10, height=3, command=lambda: press(3))
button_4 = tk.Button(root, text="4", width=10, height=3, command=lambda: press(4))
button_5 = tk.Button(root, text="5", width=10, height=3, command=lambda: press(5))
button_6 = tk.Button(root, text="6", width=10, height=3, command=lambda: press(6))
button_7 = tk.Button(root, text="7", width=10, height=3, command=lambda: press(7))
button_8 = tk.Button(root, text="8", width=10, height=3, command=lambda: press(8))
button_9 = tk.Button(root, text="9", width=10, height=3, command=lambda: press(9))
button_0 = tk.Button(root, text="0", width=10, height=3, command=lambda: press(0))
# Create buttons for operations
button_add = tk.Button(root, text="+", width=10, height=3, command=lambda: press("+"))
button_subtract = tk.Button(root, text="-", width=10, height=3, command=lambda: press("-"))
button_multiply = tk.Button(root, text="*", width=10, height=3, command=lambda: press("*"))
button_divide = tk.Button(root, text="/", width=10, height=3, command=lambda: press("/"))
In this code, we create buttons for numbers 0-9 and the four basic operations. The command option allows us to specify a function (which we’ll define next) that will be called when the button is clicked.
5. Displaying the Output
Next, we need to create a display to show the numbers entered by the user and the result of the calculations. We can use the Entry widget from Tkinter for this purpose.
# Create the display for the calculator
display = tk.Entry(root, width=20, font=("Arial", 24), bd=10, relief="sunken", justify="right")
display.grid(row=0, column=0, columnspan=4)
Here, we create an Entry widget for the display. We set its width, font size, border width, and other properties. The grid() method is used to place the display on the window.
6. Handling Button Presses and Calculations
Now comes the fun part! We need to define the press() function, which will be called when a button is pressed. This function will update the display with the corresponding number or operation and also handle the calculation when the "=" button is pressed.
current_expression = ""
def press(value):
global current_expression
current_expression += str(value)
display.delete(0, tk.END)
display.insert(tk.END, current_expression)
def calculate():
try:
result = str(eval(current_expression)) # Evaluate the expression
display.delete(0, tk.END)
display.insert(tk.END, result)
except:
display.delete(0, tk.END)
display.insert(tk.END, "Error")
The press() function adds the pressed value to the current expression, and updates the display. The calculate() function evaluates the expression using Python’s built-in eval() function and displays the result.
7. Putting Everything Together
Now that we have all the components, let’s put everything together. The last step is to organize the buttons in a grid layout and run the Tkinter main loop to display the window.
# Organize the buttons in a grid layout button_1.grid(row=1, column=0) button_2.grid(row=1, column=1) button_3.grid(row=1, column=2) button_4.grid(row=2, column=0) button_5.grid(row=2, column=1) button_6.grid(row=2, column=2) button_7.grid(row=3, column=0) button_8.grid(row=3, column=1) button_9.grid(row=3, column=2) button_0.grid(row=4, column=1) button_add.grid(row=1, column=3) button_subtract.grid(row=2, column=3) button_multiply.grid(row=3, column=3) button_divide.grid(row=4, column=3) # Add equal and clear buttons button_equals = tk.Button(root, text="=", width=10, height=3, command=calculate) button_equals.grid(row=5, column=0, columnspan=2) button_clear = tk.Button(root, text="C", width=10, height=3, command=lambda: clear()) button_clear.grid(row=5, column=2, columnspan=2) # Start the Tkinter main loop root.mainloop()
That’s it! You’ve now created a simple calculator with a GUI in Python. You can add more features, like keyboard support or advanced operations, as needed.
8. Conclusion
In this article, we walked through the process of creating a Python calculator with a GUI using Tkinter. We covered everything from setting up the window to adding buttons and handling calculations. Now, you can build your own calculator, customize it further, and even add more advanced features if desired. Happy coding!

Komentarze (0) - Nikt jeszcze nie komentował - bądź pierwszy!