Question: Tasks Using the Simple Calculator + program developed for Programming Project 4 , you will add some additional functionality and write a report about it

Tasks
Using the Simple Calculator+ program developed for Programming Project 4, you will add some additional functionality and write a report about it.
IMPORTANT: Before you begin, copy the code for Programming Project 4 to a new file named simple_calc++. py to use for this project.
Required:
Add a set of radio buttons that allow the user to choose how the numbers are displayed. The buttons should allow
- Normal floating point numbers
- Scientific notation
- Percentage
When a user chooses one of the radio buttons, the display should change to the chosen format. There will be some challenges in implementing this button. You are free to choose how it works otherwise, as long as when a user makes a choice, the number in the Entry changes to the chosen number format.
Below is my simple calculator code:
import tkinter as tk
from math import pi, e, sqrt, factorial, pow
def insert_pi():
input_field.delete(0, tk.END) # Clear the input field
input_field.insert(0, str(pi)) # Insert
# Insert the value of e into the input field
def insert_e():
input_field.delete(0, tk.END) # Clear the input field
input_field.insert(0, str(e)) # Insert e
# Calculate the square of the number in the input field
def calculate_square():
try:
number = float(input_field.get()) # Get the number
result = number **2 # Calculate square
input_field.delete(0, tk.END) # Clear input field
input_field.insert(0, str(result)) # Display result
except ValueError:
input_field.insert(0, "Error") # Display error if invalid input
# Calculate the cube of the number in the input field
def calculate_cube():
try:
number = float(input_field.get()) # Get the number
result = number **3 # Calculate cube
input_field.delete(0, tk.END) # Clear input field
input_field.insert(0, str(result)) # Display result
except ValueError:
input_field.insert(0, "Error") # Display error if invalid input
# Calculate the square root of the number in the input field
def calculate_square_root():
try:
number = float(input_field.get()) # Get the number
result = sqrt(number) # Calculate square root
input_field.delete(0, tk.END) # Clear input field
input_field.insert(0, str(result)) # Display result
except ValueError:
input_field.insert(0, "Error") # Display error if invalid input
# Calculate the factorial of the number in the input field
def calculate_factorial():
try:
number = int(input_field.get()) # Get the number
result = factorial(number) # Calculate factorial
input_field.delete(0, tk.END) # Clear input field
input_field.insert(0, str(result)) # Display result
except ValueError:
input_field.insert(0, "Error") # Display error if invalid input
# Calculate 10 raised to the power of the number in the input field
def calculate_power_of_10():
try:
number = float(input_field.get()) # Get the number
result = pow(10, number) # Calculate 10^number
input_field.delete(0, tk.END) # Clear input field
input_field.insert(0, str(result)) # Display result
except ValueError:
input_field.insert(0, "Error") # Display error if invalid input
# Append a number or character to the input field
def append_number(value):
current_value = input_field.get() # Get current value in input field
input_field.delete(0, tk.END) # Clear the field
input_field.insert(0, current_value + str(value)) # Append the new value
# Clear the input field
def clear_input():
input_field.delete(0, tk.END)
# Evaluate the expression in the input field
def evaluate_expression():
try:
result = eval(input_field.get()) # Evaluate the expression
input_field.delete(0, tk.END) # Clear input field
input_field.insert(0, str(result)) # Display result
except:
input_field.delete(0, tk.END) # Clear input field
input_field.insert(0, "Error") # Display error if invalid input
# --- Main Application Setup ---
# Create the main application window
app = tk.Tk()
app.title("Calculator") # Set window title
# Create the input field
input_field = tk.Entry(app, width=20, font=("Arial",16), borderwidth=5)
input_field.grid(row=0, column=0, columnspan=4, padx=10, pady=10) # Position input field
# --- Standard Buttons Setup ---
# Button layout (text, row, column)
button_layout =[
("7",1,0),("8",1,1),("9",1,2),("*",1,3),
("4",2,0),("5",2,1),("6",2,2),("/",2,3),
("1",3,0),("2",3,1),("3",3,2),("-",3,3),
("0",4,0),(".",4,1),("=",4,2),("+",4,3)
]
# Create standard buttons
for text, row, col in button_layout:
if text.isdigit() or text ==".": # For numeric and decimal point buttons
tk.Button(app, text=text, width=5, font=("Arial",14),
command=lambda t=text: append_number(t)).grid(row=row, column=col)
elif text =="=": # For equals button
tk.Button(app, text=text, width=5, font=("Arial",14),
command=evaluate_expression).grid(row=row, column=col)
else: # For operator buttons
tk.Button(app, text=text, width=5, font=("Arial",14),
command=lambda t=text: append_number(t)).grid(row=row, column=c
Tasks Using the Simple Calculator + program

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!