|
| 1 | +import tkinter as tk |
| 2 | + |
| 3 | + |
| 4 | +def create_sgpa_entries(): # Create SGPA entries based on the total number of semesters entered by the user. |
| 5 | + num_semesters = int(num_semesters_entry.get()) |
| 6 | + |
| 7 | + for i in range(num_semesters): |
| 8 | + sgpa_label = tk.Label(sgpa_frame, text=f"SGPA for Semester {i + 1}:") |
| 9 | + sgpa_label.grid(row=i + 2, column=0, padx=10, pady=5, sticky="E") |
| 10 | + |
| 11 | + sgpa_entry = tk.Entry(sgpa_frame) |
| 12 | + sgpa_entry.grid(row=i + 2, column=1, padx=10, pady=5, sticky="W") |
| 13 | + sgpa_entry.bind('<KeyRelease>', validate_sgpa_entries) |
| 14 | + |
| 15 | + sgpa_labels.append(sgpa_label) |
| 16 | + sgpa_entries.append(sgpa_entry) |
| 17 | + |
| 18 | + create_sgpa_buttons.grid_remove() |
| 19 | + cgpa_calc.grid(row=num_semesters + 2, column=0, columnspan=2, padx=10, pady=5) |
| 20 | + |
| 21 | + |
| 22 | +def validate_sgpa_entries(): # Validate the SGPA entries to enable or disable the CGPA calculation button. |
| 23 | + filled_entries = [entry.get() for entry in sgpa_entries] |
| 24 | + if all(filled_entries): |
| 25 | + cgpa_calc.configure(state="normal") |
| 26 | + else: |
| 27 | + cgpa_calc.configure(state="disabled") |
| 28 | + |
| 29 | + |
| 30 | +def calculate_cgpa(): # It is used to calculate cgpa. |
| 31 | + sgpa_values = [float(sgpa_entry.get()) for sgpa_entry in sgpa_entries] |
| 32 | + |
| 33 | + total_sgpa = sum(sgpa_values) |
| 34 | + num_semesters = len(sgpa_values) |
| 35 | + cgpa = total_sgpa / num_semesters |
| 36 | + |
| 37 | + cgpa_label.configure(text=f"CGPA: {cgpa:.2f}") |
| 38 | + |
| 39 | + reset_button.configure(state="normal") |
| 40 | + |
| 41 | + |
| 42 | +def reset_entries(): # This is used to reset entries after calcuting Cgpa. |
| 43 | + for label in sgpa_labels: |
| 44 | + label.destroy() |
| 45 | + for entry in sgpa_entries: |
| 46 | + entry.destroy() |
| 47 | + sgpa_labels.clear() |
| 48 | + sgpa_entries.clear() |
| 49 | + |
| 50 | + create_sgpa_buttons.grid(row=1, column=0, columnspan=2, padx=10, pady=5) |
| 51 | + cgpa_calc.configure(state="disabled") |
| 52 | + cgpa_label.configure(text="CGPA: ") |
| 53 | + |
| 54 | + reset_button.configure(state="disabled") |
| 55 | + |
| 56 | + |
| 57 | +root = tk.Tk() |
| 58 | +root.title("SGPA to CGPA Converter") |
| 59 | + |
| 60 | +sgpa_frame = tk.Frame(root) |
| 61 | +sgpa_frame.pack(pady=10) |
| 62 | + |
| 63 | +num_semesters_label = tk.Label(sgpa_frame, text="Total Number of Semesters:") |
| 64 | +num_semesters_label.grid(row=0, column=0, padx=10, pady=5, sticky="E") |
| 65 | + |
| 66 | +num_semesters_entry = tk.Entry(sgpa_frame) |
| 67 | +num_semesters_entry.grid(row=0, column=1, padx=10, pady=5, sticky="W") |
| 68 | + |
| 69 | +sgpa_labels = [] |
| 70 | +sgpa_entries = [] |
| 71 | + |
| 72 | +create_sgpa_buttons = tk.Button(sgpa_frame, text="Create SGPA Entries", command=create_sgpa_entries) |
| 73 | +create_sgpa_buttons.grid(row=1, column=0, columnspan=2, padx=10, pady=5) |
| 74 | + |
| 75 | +cgpa_calc = tk.Button(sgpa_frame, text="Calculate CGPA", command=calculate_cgpa, state="disabled") |
| 76 | + |
| 77 | +cgpa_label = tk.Label(root, text="CGPA: ") |
| 78 | +cgpa_label.pack() |
| 79 | + |
| 80 | +reset_button = tk.Button(root, text="Reset", command=reset_entries, state="disabled") |
| 81 | +reset_button.pack() |
| 82 | + |
| 83 | +root.mainloop() |
0 commit comments