In this tutorial, we will be creating a simple calculator using Python and the Tkinter library. Tkinter is the standard GUI toolkit for Python, and it provides a simple way to create graphical user interfaces.
To start off, make sure you have Python installed on your computer. You can check if you have Python installed by opening a terminal window and typing python --version
. If you don’t have Python installed, you can download it from the official Python website.
Once you have Python installed, you can start creating your calculator program. Open your favorite code editor and create a new Python file (you can name it calculator.py
or any other name you prefer).
First, we need to import the Tkinter library and create a window for our calculator. Here is the initial code for creating a Tkinter window:
import tkinter as tk
# Create a window
root = tk.Tk()
root.title("Calculator")
# Run the main loop
root.mainloop()
When you run this code, you’ll see a blank window with the title "Calculator" pop up on your screen. Next, we’ll add the buttons and display for our calculator.
Let’s start by adding a text entry widget to display the numbers and results. We’ll also add buttons for the digits 0-9 and the basic arithmetic operations (+, -, *, /).
# Create the text entry for displaying input and results
entry = tk.Entry(root, width=20, borderwidth=5)
entry.grid(row=0, column=0, columnspan=4)
# Create buttons for digits 0-9
for i in range(10):
button = tk.Button(root, text=str(i), padx=20, pady=10)
button.grid(row=(i // 3) + 1, column=(i % 3))
In the code above, we created a text entry widget at the top of the window and set up buttons for digits 0-9. You can run the code to see the initial layout of your calculator.
Next, we’ll add buttons for the arithmetic operations (+, -, *, /) and the equals button (=) to perform calculations.
# Create buttons for arithmetic operations
operators = ['+', '-', '*', '/']
for i, op in enumerate(operators):
button = tk.Button(root, text=op, padx=20, pady=10)
button.grid(row=i + 1, column=3)
# Create the equals button
equals_button = tk.Button(root, text='=', padx=20, pady=10)
equals_button.grid(row=4, column=3)
Now that we have all the elements in place, we need to add functionality to the buttons on our calculator. We will create functions to handle button clicks and perform calculations.
def button_click(num):
current = entry.get()
entry.delete(0, tk.END)
entry.insert(0, current + num)
def clear():
entry.delete(0, tk.END)
def calculate():
result = eval(entry.get())
entry.delete(0, tk.END)
entry.insert(0, str(result))
Finally, we need to bind these functions to the buttons on our calculator.
# Bind button clicks to functions
for i in range(10):
button = root.grid_slaves(row=(i // 3) + 1, column=(i % 3))[0]
button.config(command=lambda num=str(i): button_click(num))
for i, op in enumerate(operators):
button = root.grid_slaves(row=i + 1, column=3)[0]
button.config(command=lambda op=op: button_click(op))
equals_button.config(command=calculate)
And that’s it! You now have a simple calculator created using Python and Tkinter. You can run the code to see the calculator window pop up on your screen and perform basic arithmetic calculations.
Feel free to customize the layout and functionality of the calculator to suit your needs. You can add more buttons for advanced functions, change the colors and styles of the widgets, or optimize the code for better performance.
I hope you found this tutorial helpful in creating a calculator with Python and Tkinter. Happy coding!
Make a video on Java
Source code
Please give source code
Anil
Nice