|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import messagebox |
| 3 | + |
| 4 | +# ========================= |
| 5 | +# THEME |
| 6 | +# ========================= |
| 7 | +APP_BG = "#121212" |
| 8 | +PANEL_BG = "#1F1F1F" |
| 9 | +BTN_BG = "#2C2C2C" |
| 10 | +BTN_HOVER = "#3A3A3A" |
| 11 | +BTN_ACTIVE = "#FF6F61" |
| 12 | +ACCENT = "#FF6F61" |
| 13 | +TEXT_CLR = "#E0E0E0" |
| 14 | +SUBTEXT_CLR = "#AAAAAA" |
| 15 | +INPUT_BG = "#333333" |
| 16 | +INPUT_FG = "#FFFFFF" |
| 17 | + |
| 18 | +# ========================= |
| 19 | +# APP |
| 20 | +# ========================= |
| 21 | +class StudentGradeApp: |
| 22 | + def __init__(self, root): |
| 23 | + self.root = root |
| 24 | + root.title("MateTools – Student Grade Calculator") |
| 25 | + root.geometry("1080x740") |
| 26 | + root.configure(bg=APP_BG) |
| 27 | + root.resizable(False, False) |
| 28 | + |
| 29 | + # ========================= |
| 30 | + # LEFT PANEL (Actions & Inputs) |
| 31 | + # ========================= |
| 32 | + left = tk.Frame(root, bg=PANEL_BG, width=420) |
| 33 | + left.pack(side="left", fill="y") |
| 34 | + |
| 35 | + header = tk.Frame(left, bg=PANEL_BG) |
| 36 | + header.pack(fill="x", padx=16, pady=(18, 10)) |
| 37 | + |
| 38 | + tk.Label( |
| 39 | + header, |
| 40 | + text="MateTools", |
| 41 | + bg=PANEL_BG, |
| 42 | + fg=ACCENT, |
| 43 | + font=("Segoe UI", 20, "bold") |
| 44 | + ).pack(side="left") |
| 45 | + |
| 46 | + tk.Frame(left, bg=ACCENT, height=2).pack(fill="x", padx=16, pady=(0, 14)) |
| 47 | + |
| 48 | + tk.Label( |
| 49 | + left, |
| 50 | + text="Student Grade Calculator", |
| 51 | + bg=PANEL_BG, |
| 52 | + fg=TEXT_CLR, |
| 53 | + font=("Segoe UI", 14, "bold") |
| 54 | + ).pack(anchor="w", padx=16, pady=(0, 2)) |
| 55 | + |
| 56 | + tk.Label( |
| 57 | + left, |
| 58 | + text="Enter up to 5 scores (0-100)", |
| 59 | + bg=PANEL_BG, |
| 60 | + fg=SUBTEXT_CLR, |
| 61 | + font=("Segoe UI", 10) |
| 62 | + ).pack(anchor="w", padx=16, pady=(0, 16)) |
| 63 | + |
| 64 | + tk.Frame(left, bg=BTN_BG, height=1).pack(fill="x", padx=16, pady=(0, 16)) |
| 65 | + |
| 66 | + # ===== Score Entries ===== |
| 67 | + self.scores_entries = [] |
| 68 | + scores_frame = tk.Frame(left, bg=PANEL_BG) |
| 69 | + scores_frame.pack(fill="x", padx=16, pady=(0, 16)) |
| 70 | + |
| 71 | + for i in range(5): |
| 72 | + tk.Label( |
| 73 | + scores_frame, |
| 74 | + text=f"Score {i+1}:", |
| 75 | + bg=PANEL_BG, |
| 76 | + fg=SUBTEXT_CLR, |
| 77 | + font=("Segoe UI", 10, "bold") |
| 78 | + ).pack(anchor="w", pady=(2, 0)) |
| 79 | + entry = tk.Entry(scores_frame, bg=INPUT_BG, fg=INPUT_FG, font=("Segoe UI", 12)) |
| 80 | + entry.pack(fill="x", pady=(0, 5)) |
| 81 | + self.scores_entries.append(entry) |
| 82 | + |
| 83 | + # ===== Buttons ===== |
| 84 | + btn_frame = tk.Frame(left, bg=PANEL_BG) |
| 85 | + btn_frame.pack(fill="x", padx=16, pady=16) |
| 86 | + |
| 87 | + def make_btn(text, cmd, color=BTN_BG): |
| 88 | + btn = tk.Button( |
| 89 | + btn_frame, |
| 90 | + text=text, |
| 91 | + command=cmd, |
| 92 | + bg=color, |
| 93 | + fg="white", |
| 94 | + font=("Segoe UI", 11, "bold"), |
| 95 | + relief="flat", |
| 96 | + height=2, |
| 97 | + width=20 |
| 98 | + ) |
| 99 | + btn.bind("<Enter>", lambda e, b=btn: b.config(bg=BTN_HOVER)) |
| 100 | + btn.bind("<Leave>", lambda e, b=btn, c=color: b.config(bg=c)) |
| 101 | + btn.bind("<ButtonPress-1>", lambda e, b=btn: b.config(bg=BTN_ACTIVE)) |
| 102 | + btn.bind("<ButtonRelease-1>", lambda e, b=btn, c=color: b.config(bg=BTN_HOVER)) |
| 103 | + return btn |
| 104 | + |
| 105 | + make_btn("Calculate Grade", self.calculate_grade, ACCENT).pack(side="top", pady=8) |
| 106 | + make_btn("Clear", self.clear_fields, BTN_BG).pack(side="top", pady=8) |
| 107 | + make_btn("About", self.show_about, BTN_BG).pack(side="top", pady=20) |
| 108 | + |
| 109 | + # ========================= |
| 110 | + # RIGHT PANEL (Results & History) |
| 111 | + # ========================= |
| 112 | + right = tk.Frame(root, bg=APP_BG) |
| 113 | + right.pack(side="right", fill="both", expand=True) |
| 114 | + |
| 115 | + self.result_card = tk.Frame(right, bg=PANEL_BG, bd=2, relief="ridge") |
| 116 | + self.result_card.pack(padx=30, pady=20, fill="both", expand=True) |
| 117 | + |
| 118 | + # ---------- TOP: App Label ---------- |
| 119 | + tk.Label( |
| 120 | + self.result_card, |
| 121 | + text="Student Grade Calculator", |
| 122 | + bg=PANEL_BG, |
| 123 | + fg=TEXT_CLR, |
| 124 | + font=("Segoe UI", 18, "bold") |
| 125 | + ).pack(pady=(20, 10)) |
| 126 | + |
| 127 | + # ---------- MAIN FRAME ---------- |
| 128 | + main_frame = tk.Frame(self.result_card, bg=PANEL_BG) |
| 129 | + main_frame.pack(fill="both", expand=True, padx=20, pady=10) |
| 130 | + |
| 131 | + # ===== LEFT COLUMN: Result ===== |
| 132 | + left_frame = tk.Frame(main_frame, bg=PANEL_BG) |
| 133 | + left_frame.pack(side="left", fill="y", padx=(0, 15)) |
| 134 | + |
| 135 | + tk.Label( |
| 136 | + left_frame, |
| 137 | + text="Latest Grade:", |
| 138 | + bg=PANEL_BG, |
| 139 | + fg=SUBTEXT_CLR, |
| 140 | + font=("Segoe UI", 12, "bold") |
| 141 | + ).pack(anchor="w", pady=(0, 5)) |
| 142 | + |
| 143 | + self.result_label = tk.Label( |
| 144 | + left_frame, |
| 145 | + text="Grade: --", |
| 146 | + bg=PANEL_BG, |
| 147 | + fg=ACCENT, |
| 148 | + font=("Segoe UI", 18, "bold"), |
| 149 | + width=20 |
| 150 | + ) |
| 151 | + self.result_label.pack(anchor="w", pady=(0, 10)) |
| 152 | + |
| 153 | + # ===== RIGHT COLUMN: History ===== |
| 154 | + right_frame = tk.Frame(main_frame, bg=PANEL_BG) |
| 155 | + right_frame.pack(side="right", fill="both", expand=True) |
| 156 | + |
| 157 | + history_card = tk.Frame(right_frame, bg="#1C1C1C", bd=1, relief="ridge") |
| 158 | + history_card.pack(fill="both", expand=True) |
| 159 | + |
| 160 | + tk.Label( |
| 161 | + history_card, |
| 162 | + text="History", |
| 163 | + bg="#1C1C1C", |
| 164 | + fg=SUBTEXT_CLR, |
| 165 | + font=("Segoe UI", 12, "bold") |
| 166 | + ).pack(anchor="w", padx=10, pady=(5, 0)) |
| 167 | + |
| 168 | + self.history_box = tk.Listbox( |
| 169 | + history_card, |
| 170 | + bg=INPUT_BG, |
| 171 | + fg=INPUT_FG, |
| 172 | + font=("Segoe UI", 12), |
| 173 | + height=20, |
| 174 | + selectbackground=ACCENT, |
| 175 | + relief="flat" |
| 176 | + ) |
| 177 | + self.history_box.pack(fill="both", expand=True, padx=10, pady=(0, 10)) |
| 178 | + |
| 179 | + self.history = [] |
| 180 | + |
| 181 | + # ========================= |
| 182 | + # METHODS |
| 183 | + # ========================= |
| 184 | + def calculate_grade(self): |
| 185 | + try: |
| 186 | + scores = [float(entry.get()) for entry in self.scores_entries if entry.get() != ""] |
| 187 | + if not scores: |
| 188 | + raise ValueError("No scores entered") |
| 189 | + average = sum(scores) / len(scores) |
| 190 | + |
| 191 | + if average >= 90: |
| 192 | + grade = "A" |
| 193 | + elif average >= 80: |
| 194 | + grade = "B" |
| 195 | + elif average >= 70: |
| 196 | + grade = "C" |
| 197 | + elif average >= 60: |
| 198 | + grade = "D" |
| 199 | + else: |
| 200 | + grade = "F" |
| 201 | + |
| 202 | + result_text = f"{grade} ({average:.2f}%)" |
| 203 | + self.result_label.config(text=f"Grade: {result_text}") |
| 204 | + |
| 205 | + # Add to history (last 25) |
| 206 | + entry = f"Scores: {', '.join([str(int(s)) for s in scores])} -> Grade: {result_text}" |
| 207 | + self.history.append(entry) |
| 208 | + self.history = self.history[-25:] |
| 209 | + self.update_history() |
| 210 | + except ValueError: |
| 211 | + messagebox.showerror("Error", "Please enter valid numeric scores (0-100)") |
| 212 | + |
| 213 | + def update_history(self): |
| 214 | + self.history_box.delete(0, tk.END) |
| 215 | + for item in self.history: |
| 216 | + self.history_box.insert(tk.END, item) |
| 217 | + |
| 218 | + def clear_fields(self): |
| 219 | + for entry in self.scores_entries: |
| 220 | + entry.delete(0, tk.END) |
| 221 | + self.result_label.config(text="Grade: --") |
| 222 | + self.history = [] |
| 223 | + self.update_history() |
| 224 | + |
| 225 | + def show_about(self): |
| 226 | + messagebox.showinfo( |
| 227 | + "About", |
| 228 | + "MateTools – Student Grade Calculator\n\n" |
| 229 | + "• Enter up to 5 scores (0-100)\n" |
| 230 | + "• Calculates average and grade\n" |
| 231 | + "• Tracks last 25 calculated grades\n\n" |
| 232 | + "Built by MateTools" |
| 233 | + ) |
| 234 | + |
| 235 | +# ========================= |
| 236 | +# RUN |
| 237 | +# ========================= |
| 238 | +if __name__ == "__main__": |
| 239 | + root = tk.Tk() |
| 240 | + StudentGradeApp(root) |
| 241 | + root.mainloop() |
0 commit comments