Here I’ll be presenting a simple python program for calculating Simple Interest.
import tkinter as tk from tkinter import messagebox
The code `import tkinter as tk` and `from tkinter import messagebox` brings in necessary elements from the `tkinter` package to construct graphical user interfaces in Python. The `tkinter` package is brought in with the nickname `tk` to simplify the utilization of its components and methods across the code. Furthermore, `messagebox` is imported directly from `tkinter` to allow for the generation of pop-up dialog boxes that can exhibit error messages or other notifications, offering a means to engage with users and manage exceptions or alerts within the GUI application.
def calculate_interest_by_Chirag(): try: principal_amount = float(principal_entry.get()) rate_of_interest = float(rate_entry.get()) time_taken_for_amount = float(time_entry.get()) SI = (principal_amount * rate_of_interest * time_taken_for_amount) / 100 result_label.config(text=f"Simple Interest: {SI:.2f}") except ValueError: messagebox.showerror("Input Error", "Please enter valid numbers.")
The `calculate_interest_by_Chirag()` function is designed to compute the simple interest using user-provided inputs from the GUI. It acquires the principal amount, rate of interest, and time period from their corresponding entry fields, converts them into floating-point numbers, and then applies the formula SI=(p*r*t)/100, to determine the simple interest. The resulting value is exhibited in the `result_label` with a precision of two decimal places. In case any of the inputs are deemed invalid (i.e., not numerical values), a `ValueError` is captured, and the user is prompted to input valid numerical values through an error message.
app = tk.Tk() app.title("Simple Interest Calculator")
The program initializes an instance of the `Tk` class from the `tkinter` module, and assigns it to the variable `app` in order to create the main application window for the GUI. This window acts as the central interface for placing all graphical elements, including labels, buttons, and entry fields. The code `app.title(“Simple Interest Calculator”)` is used to set the window’s title to “Simple Interest Calculator,” which is displayed in the title bar to assist users in understanding the application’s purpose.
tk.Label(app, text="Enter principal amount:").pack(pady=5) principal_entry = tk.Entry(app) principal_entry.pack(pady=5) tk.Label(app, text="Enter rate of interest (%):").pack(pady=5) rate_entry = tk.Entry(app) rate_entry.pack(pady=5) tk.Label(app, text="Enter time period (years):").pack(pady=5) time_entry = tk.Entry(app) time_entry.pack(pady=5) tk.Button(app, text="Calculate", command=calculate_interest_by_Chirag).pack(pady=5) result_label = tk.Label(app, text="Simple Interest: ") result_label.pack(pady=20)
This portion of the code is responsible for generating and arranging the user interface components (widgets) within the primary application window. Initially, it includes multiple labels that instruct the user to input values for the principal amount, rate of interest, and time period, each accompanied by an input field. The positioning of each widget is determined using the `pack()` method with vertical padding to maintain appropriate spacing. Subsequently, a button labeled “Calculate” is inserted, which, upon activation, calls the `calculate_interest_by_Chirag` function to calculate the simple interest. Lastly, a label is incorporated to exhibit the computed result, with additional padding for emphasis. This design ensures that the graphical user interface (GUI) is both user-friendly and well-structured.
app.mainloop()
The execution of app.mainloop() initiates the primary event loop of the application, ensuring the continuous operation and responsiveness of the GUI window. This loop is responsible for consistently managing user interactions, including button clicks and text input, and subsequently modifying the interface. In the absence of this loop, the application would promptly terminate upon launch, rendering it inoperable. Through the execution of app.mainloop(), the application sustains its functionality and interactivity until the user opts to terminate the window, enabling the GUI to effectively process and react to events throughout its runtime.