Simple Calculator Program in Python with tkinter

The rest of the code 1

The rest of the code 2

Output:

Explanation:
Imports:
tkinter as tk:
Imports the tkinter module under the alias tk
.
from tkinter import messagebox:
Imports the messagebox submodule from tkinter to show error messages.
Functions:
evaluate_expression():
Attempts to evaluate the expression entered in the entry widget (entry
). It uses Python's eval()
function to evaluate the string expression to a numerical result. If successful, it clears the entry and inserts the result. If there's an error (e.g., invalid input), it shows an error message using messagebox.showerror()
.
add_to_expression(char):
Appends the character char
(which represents a button's text) to the end of the entry widget (entry
).
clear_expression():
Clears the content of the entry widget (entry
).
Creating the Main Window (root):
tk.Tk():
Creates the main window of the application.
root.title("Simple Calculator"):
Sets the title of the window to "Simple Calculator".
Entry Widget:
tk.Entry(root, width=30, borderwidth=5):
Creates an entry widget (entry
) within the main window (root
) for displaying and entering expressions.
entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10):
Places the entry widget in the first row, spanning all four columns with padding around it.
Buttons:
- Defined as a list of tuples (
buttons
) where each tuple contains the text to display on the button, its row position, and column position.
- Buttons are created using a loop (
for (text, row, col) in buttons
), where each button is placed in its respective row and column using tk.Button()
.
Clear (C
) and Evaluate (=
) Buttons:
tk.Button(root, text='C', width=7, height=3, command=clear_expression):
Creates a button labeled "C" that clears the entry when clicked (command=clear_expression
).
tk.Button(root, text='=', width=7, height=3, command=evaluate_expression):
Creates a button labeled "=" that evaluates the expression and displays the result when clicked (command=evaluate_expression
).
Main Loop:
evaluate_expression():
Attempts to evaluate the expression entered in the entry widget (entry
). It uses Python's eval()
function to evaluate the string expression to a numerical result. If successful, it clears the entry and inserts the result. If there's an error (e.g., invalid input), it shows an error message using messagebox.showerror()
.add_to_expression(char):
Appends the character char
(which represents a button's text) to the end of the entry widget (entry
).clear_expression():
Clears the content of the entry widget (entry
).tk.Tk():
Creates the main window of the application.root.title("Simple Calculator"):
Sets the title of the window to "Simple Calculator".
Entry Widget:
tk.Entry(root, width=30, borderwidth=5):
Creates an entry widget (entry
) within the main window (root
) for displaying and entering expressions.
entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10):
Places the entry widget in the first row, spanning all four columns with padding around it.
Buttons:
- Defined as a list of tuples (
buttons
) where each tuple contains the text to display on the button, its row position, and column position.
- Buttons are created using a loop (
for (text, row, col) in buttons
), where each button is placed in its respective row and column using tk.Button()
.
Clear (C
) and Evaluate (=
) Buttons:
tk.Button(root, text='C', width=7, height=3, command=clear_expression):
Creates a button labeled "C" that clears the entry when clicked (command=clear_expression
).
tk.Button(root, text='=', width=7, height=3, command=evaluate_expression):
Creates a button labeled "=" that evaluates the expression and displays the result when clicked (command=evaluate_expression
).
Main Loop:
tk.Entry(root, width=30, borderwidth=5):
Creates an entry widget (entry
) within the main window (root
) for displaying and entering expressions.entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10):
Places the entry widget in the first row, spanning all four columns with padding around it.- Defined as a list of tuples (
buttons
) where each tuple contains the text to display on the button, its row position, and column position. - Buttons are created using a loop (
for (text, row, col) in buttons
), where each button is placed in its respective row and column usingtk.Button()
.
Clear (C
) and Evaluate (=
) Buttons:
tk.Button(root, text='C', width=7, height=3, command=clear_expression):
Creates a button labeled "C" that clears the entry when clicked (command=clear_expression
).
tk.Button(root, text='=', width=7, height=3, command=evaluate_expression):
Creates a button labeled "=" that evaluates the expression and displays the result when clicked (command=evaluate_expression
).
Main Loop:
tk.Button(root, text='C', width=7, height=3, command=clear_expression):
Creates a button labeled "C" that clears the entry when clicked (command=clear_expression
).tk.Button(root, text='=', width=7, height=3, command=evaluate_expression):
Creates a button labeled "=" that evaluates the expression and displays the result when clicked (command=evaluate_expression
).root.mainloop():
Starts the main event loop of the tkinter application, which listens for events (such as button clicks) and handles them.