Creating a Desktop Calculator Application using Tkinter
Initializing the Application Window
The tkinter library provides a standard interface for building desktop applications in Python. Start by initializing the main window with specific dimensions and a title.
import tkinter as tk
root = tk.Tk()
root.title("Python GUI Calculator")
root.geometry("450x400")
Designing the Input Interface
Use Entry widgets to capture user input. Labels help identify where the user should type the numerical values.
header = tk.Label(root, text="Arithmetic Tool\n-----------------", width=20, height=3)
header.pack()
# First Number Input
tk.Label(root, text="Value 1:").place(x=40, y=100)
val1_entry = tk.Entry(root, width=12)
val1_entry.place(x=110, y=100)
# Second Number Input
tk.Label(root, text="Value 2:").place(x=40, y=140)
val2_entry = tk.Entry(root, width=12)
val2_entry.place(x=110, y=140)
Configuring the Result Display
To display the calculated value dynamically, use a DoubleVar linked to a label. This allows the UI to update automatically when the variable's value changes.
calc_result = tk.DoubleVar()
tk.Label(root, text="Calculated Result", font=("Arial", 10, "bold")).place(x=150, y=220)
display_box = tk.Label(root, textvariable=calc_result, width=20, height=4, relief="sunken")
display_box.place(x=130, y=260)
Implementing Calculation Logic
Define functions to perform arithmetic operations. These functions retrieve values from the entry fields, convert them to floats for precision, and update the display variable.
def perform_addition():
a = float(val1_entry.get())
b = float(val2_entry.get())
calc_result.set(a + b)
def perform_subtraction():
a = float(val1_entry.get())
b = float(val2_entry.get())
calc_result.set(a - b)
def perform_multiplication():
a = float(val1_entry.get())
b = float(val2_entry.get())
calc_result.set(a * b)
def perform_division():
a = float(val1_entry.get())
b = float(val2_entry.get())
if b != 0:
calc_result.set(a / b)
else:
calc_result.set(0.0)
Adding Interactive Buttons
Create buttons and assign the logical functions to the command attribute to handle user clicks.
# Action buttons organized by position
btn_add = tk.Button(root, text="Add (+)", width=12, command=perform_addition)
btn_add.place(x=280, y=100)
btn_sub = tk.Button(root, text="Subtract (-)", width=12, command=perform_subtraction)
btn_sub.place(x=280, y=140)
btn_mul = tk.Button(root, text="Multiply (*)", width=12, command=perform_multiplication)
btn_mul.place(x=280, y=180)
btn_div = tk.Button(root, text="Divide (/)", width=12, command=perform_division)
btn_div.place(x=280, y=220)
Complete Implementation
Below is the consolidated code required to run the application.
import tkinter as tk
root = tk.Tk()
root.title("Python GUI Calculator")
root.geometry("450x400")
# UI Headers
header = tk.Label(root, text="Arithmetic Tool\n-----------------", width=20, height=3)
header.pack()
# Input Fields
tk.Label(root, text="Value 1:").place(x=40, y=100)
val1_entry = tk.Entry(root, width=12)
val1_entry.place(x=110, y=100)
tk.Label(root, text="Value 2:").place(x=40, y=140)
val2_entry = tk.Entry(root, width=12)
val2_entry.place(x=110, y=140)
# Output Variable
calc_result = tk.DoubleVar()
tk.Label(root, text="Calculated Result", font=("Arial", 10, "bold")).place(x=150, y=220)
display_box = tk.Label(root, textvariable=calc_result, width=20, height=4, relief="sunken")
display_box.place(x=130, y=260)
# Logic
def perform_addition():
calc_result.set(float(val1_entry.get()) + float(val2_entry.get()))
def perform_subtraction():
calc_result.set(float(val1_entry.get()) - float(val2_entry.get()))
def perform_multiplication():
calc_result.set(float(val1_entry.get()) * float(val2_entry.get()))
def perform_division():
b = float(val2_entry.get())
if b != 0:
calc_result.set(float(val1_entry.get()) / b)
# Buttons
tk.Button(root, text="Add (+)", width=12, command=perform_addition).place(x=280, y=100)
tk.Button(root, text="Subtract (-)", width=12, command=perform_subtraction).place(x=280, y=140)
tk.Button(root, text="Multiply (*)", width=12, command=perform_multiplication).place(x=280, y=180)
tk.Button(root, text="Divide (/)", width=12, command=perform_division).place(x=280, y=220)
root.mainloop()