Data Processing
To create a student calling application, we need to extract student names and IDs from an Excel speradsheet. This example uses the pandas library to read data.
pip install pandas
pip install openpyxl
The input Excel file (demo.xlsx) contains columns such as "ID" and "Name". We load and validate the data:
import pandas as pd
def process_student_data(file_path):
df = pd.read_excel(file_path)
columns = df.columns.tolist()
assert "ID" in columns, "Column named 'ID' is required!"
assert "Name" in columns, "Column named 'Name' is required!"
return [f"{row['ID']} {row['Name']}" for _, row in df.iterrows()]
GUI Construction
Using tkinter, we construct a graphical interface for the application.
import tkinter as tk
import random
running = False
class CallRollApp(tk.Tk):
def __init__(self, student_list):
super().__init__()
self.student_list = student_list
self.running = False
self.title("Student Selector")
self.geometry("400x300")
self.display_var = tk.StringVar(value="Ready to start")
self.display_label = tk.Label(self, textvariable=self.display_var)
self.display_label.pack(pady=20)
self.start_button = tk.Button(self, text="Start", command=self.start_selection)
self.start_button.pack(pady=10)
self.stop_button = tk.Button(self, text="Stop", command=self.stop_selection)
self.stop_button.pack(pady=10)
def roll_selection(self):
self.display_var.set(random.choice(self.student_list))
if self.running:
self.after(50, self.roll_selection)
def start_selection(self):
if not self.running:
self.running = True
self.roll_selection()
def stop_selection(self):
self.running = False
Enhanced Featurse
To improve usability, we allow dynamic file selection via a file dialog.
from tkinter import filedialog
class FileUploadWindow(tk.Tk):
def __init__(self):
super().__init__()
self.title("Upload Students File")
self.geometry("350x100")
tk.Label(self, text="File Path").grid(row=0, column=0, padx=(10, 0), pady=10)
self.file_entry = tk.Entry(self)
self.file_entry.grid(row=0, column=1, columnspan=2, padx=(0, 10), ipadx=60)
tk.Button(self, text="Browse", command=self.browse_file).grid(row=2, column=1, pady=10)
tk.Button(self, text="Load Data", command=self.load_data).grid(row=2, column=2, padx=(0, 10))
def browse_file(self):
file_path = filedialog.askopenfilename(
title="Select Excel File",
filetypes=[("Excel files", "*.xlsx *.xls")]
)
self.file_entry.delete(0, tk.END)
self.file_entry.insert(0, file_path)
def load_data(self):
try:
data = process_student_data(self.file_entry.get())
self.destroy()
CallRollApp(data).mainloop()
except Exception as e:
from tkinter.messagebox import showwarning
showwarning("Error", f"Failed to load data:\n{e}")
Execution Flow
The application starts by launching the file upload window. After selecting and loading the Excel file, it transitions to the student selection interface where users can start and stop the random selection process.