🧮 Build a Calculator in Python Using Lambda & Tkinter
Creating a calculator is a great way to learn about Python functions, GUI programming, and event handling. In this tutorial, we’ll build a simple calculator using lambda functions for operations and Tkinter for the user interface.
📦 What You’ll Learn
-
How to use lambda functions for basic operations
-
How to build a Tkinter GUI
-
How to wire up buttons with actions dynamically
🛠️ Prerequisites
-
Python 3.x installed
-
Basic knowledge of Python functions and Tkinter
You can install Tkinter (if not already installed) using:
pip install tk
Note: Tkinter comes pre-installed with most Python distributions.
🔣 Step-by-Step: Create a Lambda-Based Calculator
✅ Step 1: Import Tkinter
import tkinter as tk
from functools import partial
✅ Step 2: Define Operations Using Lambda
operations = {
'+': lambda x, y: float(x) + float(y),
'-': lambda x, y: float(x) - float(y),
'*': lambda x, y: float(x) * float(y),
'/': lambda x, y: float(x) / float(y) if float(y) != 0 else "Error"
}
✅ Step 3: Create the GUI Window
root = tk.Tk()
root.title("Lambda Calculator")
root.geometry("300x300")
✅ Step 4: Create Input Fields and Result Display
entry1 = tk.Entry(root, width=10)
entry1.grid(row=0, column=0, padx=10, pady=10)
entry2 = tk.Entry(root, width=10)
entry2.grid(row=0, column=1, padx=10, pady=10)
result_label = tk.Label(root, text="Result: ")
result_label.grid(row=1, column=0, columnspan=2)
✅ Step 5: Define a Generic Operation Handler
def calculate(op):
try:
val1 = entry1.get()
val2 = entry2.get()
result = operations[op](val1, val2)
result_label.config(text=f"Result: {result}")
except Exception as e:
result_label.config(text="Error")
✅ Step 6: Create Buttons for Operations
row = 2
col = 0
for symbol in operations:
action = partial(calculate, symbol)
tk.Button(root, text=symbol, width=5, command=action).grid(row=row, column=col, padx=5, pady=5)
col += 1
✅ Step 7: Run the Application
root.mainloop()
🖥️ Full Working Code
Here’s the full code for your reference:
import tkinter as tk
from functools import partial
# Define lambda operations
operations = {
'+': lambda x, y: float(x) + float(y),
'-': lambda x, y: float(x) - float(y),
'*': lambda x, y: float(x) * float(y),
'/': lambda x, y: float(x) / float(y) if float(y) != 0 else "Error"
}
# Create the window
root = tk.Tk()
root.title("Lambda Calculator")
root.geometry("300x300")
# Entry widgets
entry1 = tk.Entry(root, width=10)
entry1.grid(row=0, column=0, padx=10, pady=10)
entry2 = tk.Entry(root, width=10)
entry2.grid(row=0, column=1, padx=10, pady=10)
# Result label
result_label = tk.Label(root, text="Result: ")
result_label.grid(row=1, column=0, columnspan=2)
# Calculation function
def calculate(op):
try:
val1 = entry1.get()
val2 = entry2.get()
result = operations[op](val1, val2)
result_label.config(text=f"Result: {result}")
except Exception:
result_label.config(text="Error")
# Buttons for operations
row = 2
col = 0
for symbol in operations:
action = partial(calculate, symbol)
tk.Button(root, text=symbol, width=5, command=action).grid(row=row, column=col, padx=5, pady=5)
col += 1
# Run the app
root.mainloop()
📚 Summary
-
Lambda functions let us define concise, anonymous operations.
-
Tkinter helps us build user interfaces with minimal code.
-
This calculator is fully functional and can be expanded to support more features like clearing fields or advanced operations.
Would you like to watch video explanation to create a calculator using python Lambda ? watch video below:
No comments:
Post a Comment