Step 1: Install Tkinter
Tkinter comes pre-installed with Python, so you don't need to install anything extra. You can start using it right away.
Step 2: Set Up the Calculator GUI
Create a new Python file named calculator.py
and import the necessary modules.
Step 3: Initialize Tkinter and Set Up the Main Window
pythonimport tkinter as tk
# Initialize the main window
root = tk.Tk()
root.title("Simple Calculator")
# Set the size of the window
root.geometry("400x500")
# Set the background color
root.configure(bg='light gray')
Step 4: Create the Display and Buttons
- Display: Create an entry widget to display the calculator's input and output.
- Buttons: Create buttons for numbers, operators, and other functions (clear, equal).
python# Entry widget for the display
display = tk.Entry(root, font=('arial', 20, 'bold'), bd=30, insertwidth=4, width=14, borderwidth=4)
display.grid(row=0, column=0, columnspan=4)
# Button click function
def button_click(number):
current = display.get()
display.delete(0, tk.END)
display.insert(0, current + str(number))
# Clear display function
def button_clear():
display.delete(0, tk.END)
# Equal function to evaluate the expression
def button_equal():
try:
result = str(eval(display.get()))
display.delete(0, tk.END)
display.insert(0, result)
except:
display.delete(0, tk.END)
display.insert(0, "Error")
# Create button function
def create_button(text, row, col, func=None, width=10, height=3, color='light blue'):
button = tk.Button(root, text=text, padx=20, pady=20, font=('arial', 18, 'bold'), bg=color, command=func, width=width, height=height)
button.grid(row=row, column=col)
# Number buttons
buttons = [
('7', 1, 0), ('8', 1, 1), ('9', 1, 2),
('4', 2, 0), ('5', 2, 1), ('6', 2, 2),
('1', 3, 0), ('2', 3, 1), ('3', 3, 2),
('0', 4, 1)
]
for (text, row, col) in buttons:
create_button(text, row, col, lambda t=text: button_click(t))
# Operator buttons
operators = [
('+', 1, 3), ('-', 2, 3), ('*', 3, 3), ('/', 4, 3),
]
for (text, row, col) in operators:
create_button(text, row, col, lambda t=text: button_click(t), color='orange')
# Other buttons
create_button('C', 4, 0, button_clear, color='red')
create_button('=', 4, 2, button_equal, color='green')
Step 5: Run the Main Loop
Finally, run the main loop to display the calculator window.
python# Run the main loop
root.mainloop()
Complete Code
Here is the complete code for the calculator:
pythonimport tkinter as tk
# Initialize the main window
root = tk.Tk()
root.title("Simple Calculator")
root.geometry("400x500")
root.configure(bg='light gray')
# Entry widget for the display
display = tk.Entry(root, font=('arial', 20, 'bold'), bd=30, insertwidth=4, width=14, borderwidth=4)
display.grid(row=0, column=0, columnspan=4)
# Button click function
def button_click(number):
current = display.get()
display.delete(0, tk.END)
display.insert(0, current + str(number))
# Clear display function
def button_clear():
display.delete(0, tk.END)
# Equal function to evaluate the expression
def button_equal():
try:
result = str(eval(display.get()))
display.delete(0, tk.END)
display.insert(0, result)
except:
display.delete(0, tk.END)
display.insert(0, "Error")
# Create button function
def create_button(text, row, col, func=None, width=10, height=3, color='light blue'):
button = tk.Button(root, text=text, padx=20, pady=20, font=('arial', 18, 'bold'), bg=color, command=func, width=width, height=height)
button.grid(row=row, column=col)
# Number buttons
buttons = [
('7', 1, 0), ('8', 1, 1), ('9', 1, 2),
('4', 2, 0), ('5', 2, 1), ('6', 2, 2),
('1', 3, 0), ('2', 3, 1), ('3', 3, 2),
('0', 4, 1)
]
for (text, row, col) in buttons:
create_button(text, row, col, lambda t=text: button_click(t))
# Operator buttons
operators = [
('+', 1, 3), ('-', 2, 3), ('*', 3, 3), ('/', 4, 3),
]
for (text, row, col) in operators:
create_button(text, row, col, lambda t=text: button_click(t), color='orange')
# Other buttons
create_button('C', 4, 0, button_clear, color='red')
create_button('=', 4, 2, button_equal, color='green')
# Run the main loop
root.mainloop()
0 Comments