""" Copyright (c) 2023 Grader Than Technology LLC. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Description: This is a GUI calculator application built with python tkinter that is able to add, subtract multiple and divide. Author: Harris Williams """ from tkinter import * from tkinter import messagebox # Create the main window for the calculator root = Tk() # Set the title of the window to "Simple Calculator" root.title("Simple Calculator") # Set the minimum size of the window to 250x400 pixels root.minsize(250, 400) # The above code creates a new Tkinter window object named "root", sets the window title to "Simple Calculator", # and sets the minimum size of the window to 250x400 pixels. The window is currently empty, and we need to add # the calculator buttons and display to it in the rest of the code. # Create the display for the calculator display = Entry(root, width=35, borderwidth=5) # Place the display on the window grid display.grid(row=0, column=0, columnspan=4, padx=10, pady=10 , sticky='ew') # The above code creates an Entry widget named "display" that will be used to input numbers and display the calculator # result. The widget is given a width of 35 characters and a borderwidth of 5 pixels. # Function to handle button clicks and update the display def button_click(number): """ Update the display with the pressed button number. Parameters: number (int): The number to be added to the display. Returns: None """ current = display.get() display.delete(0, END) display.insert(0, str(current) + str(number)) # Function to handle the addition button def button_add(): """ Store the first number, set the math operation to addition, and clear the display. Parameters: None Returns: None """ first_number = display.get() global f_num global math_operation math_operation = "addition" f_num = int(first_number) display.delete(0, END) # Function to handle the equal button def button_equal(): """ Perform the selected math operation and update the display with the result. Parameters: None Returns: None """ second_number = display.get() display.delete(0, END) if math_operation == "addition": display.insert(0, f_num + int(second_number)) if math_operation == "subtraction": display.insert(0, f_num - int(second_number)) if math_operation == "multiplication": display.insert(0, f_num * int(second_number)) if math_operation == "division": try: display.insert(0, f_num / int(second_number)) except ZeroDivisionError: messagebox.showerror("Error", "Cannot divide by zero") # Function to handle the clear button def button_clear(): """ Clear the display. Parameters: None Returns: None """ display.delete(0, END) # Function to handle the subtraction button def button_subtract(): """ Store the first number, set the math operation to subtraction, and clear the display. Parameters: None Returns: None """ first_number = display.get() global f_num global math_operation math_operation = "subtraction" f_num = int(first_number) display.delete(0, END) # Function to handle the multiplication button def button_multiply(): """ Store the first number, set the math operation to multiplication, and clear the display. Parameters: None Returns: None """ first_number = display.get() global f_num global math_operation math_operation = "multiplication" f_num = int(first_number) display.delete(0, END) # Function to handle the division button def button_divide(): """ Store the first number, set the math operation to division, and clear the display. Parameters: None Returns: None """ first_number = display.get() global f_num global math_operation math_operation = "division" f_num = int(first_number) display.delete(0, END) # Create a list to store the number buttons number_buttons = [] # Loop through the number labels and create a button for each number for label in range(10): # Create the button with the corresponding label and command button = Button(root, text=label, padx=40, pady=20, command=lambda num=label: button_click(num)) # Add the button to the list of number buttons and place it on the calculator window if label == 0: button.grid(row=4, column=0, sticky='ew') else: number_buttons.append(button) row = (label - 1) // 3 + 1 column = (label - 1) % 3 number_buttons[-1].grid(row=row, column=column, sticky='ew') button_add = Button(root, text="+", padx=40, pady=20, command=button_add) button_equal = Button(root, text="=", padx=40, pady=20, command=button_equal) button_clear = Button(root, text="Clear", padx=40, pady=20, command=button_clear) button_subtract = Button(root, text="-", padx=40, pady=20, command=button_subtract) button_multiply = Button(root, text="*", padx=40, pady=20, command=button_multiply) button_divide = Button(root, text="/", padx=40, pady=20, command=button_divide) button_clear.grid(row=4, column=1, columnspan=2, sticky='ew') button_add.grid(row=5, column=0, sticky='ew') button_equal.grid(row=5, column=1, columnspan=2, sticky='ew') button_subtract.grid(row=6, column=0, sticky='ew') button_multiply.grid(row=6, column=1, sticky='ew') button_divide.grid(row=6, column=2, sticky='ew') root.mainloop()