Conversation with Merlin [email protected] · Wed Dec 06 2023

import customtkinter as ctk from customtkinter import * import tkinter as tk from tkinter import filedialog import tkinter.messagebox as tkmb import csv

Function to register a new student

def register_student(): with open('students.csv', mode='a', newline='') as file: writer = csv.writer(file) writer.writerow([student_name.get(), roll_number.get(), semester.get(), branch.get(), course_title.get()]) tkmb.showinfo("Success", "Student details added successfully")

Function to add data from a selected CSV file

def choose_file_and_update(): filepath = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv")]) if filepath: with open(filepath, newline='') as csvfile: reader = csv.reader(csvfile) with open('students.csv', mode='a', newline='') as file: writer = csv.writer(file) for row in reader: writer.writerow(row) tkmb.showinfo("Success", "Data from the file added successfully")

Selecting GUI theme - dark, light, system (for system default)

ctk.set_appearance_mode("dark")

Selecting color theme - blue, green, dark-blue

ctk.set_default_color_theme("blue")

app = ctk.CTk() app.geometry("800x600") app.title("ATTENDO")

Creating the CTkTabview

tabview = ctk.CTkTabview(master=app) tabview.pack(padx=50, pady=50, fill='both', expand=True)

Add the necessary tabs

tabview.add("Register Student") tabview.add("Mark Attendance") tabview.add("View Attendance")

This is the frame for the "Register Student" tab

registration_frame = tabview.tab("Register Student")

student_name = ctk.CTkEntry(master=registration_frame, placeholder_text="Student Name") student_name.pack(pady=12, padx=10)

roll_number = ctk.CTkEntry(master=registration_frame, placeholder_text="Roll Number") roll_number.pack(pady=12, padx=10)

semester = CTkComboBox(master=registration_frame, values=["Semester","Semester 1", "Semester 2", "Semester 3","Semester 4","Semester 5","Semester 6", "Semester 7","Semester 8"]) semester.pack(pady=12, padx=10)

branch = CTkComboBox(master=registration_frame, values=["Branch Name","CSE", "ECE"]) branch.pack(pady=12, padx=10)

course_title = CTkComboBox(master=registration_frame, values=["Course Title","IPC","DCS","PGD","MOOC","QC","HCI","NNDL","NLP","AI"]) course_title.pack(pady=12, padx=10)

register_button1 = ctk.CTkButton(master=registration_frame, text='Register', command=register_student) register_button1.pack(pady=12, padx=10)

label = ctk.CTkLabel(master=registration_frame,text='OR') label.pack(pady=5,padx=5)

register_button2 = ctk.CTkButton(master=registration_frame, text='Choose Student Details File', command=choose_file_and_update) register_button2.pack(pady=12, padx=10)

This is the frame for the "Mark Attendance" tab

mark_attendance_frame = tabview.tab("Mark Attendance")

semester = CTkComboBox(master=mark_attendance_frame, values=["Semester","Semester 1", "Semester 2", "Semester 3","Semester 4","Semester 5","Semester 6", "Semester 7","Semester 8"]) semester.pack(pady=12, padx=10)

branch = CTkComboBox(master=mark_attendance_frame, values=["Branch Name","CSE", "ECE"]) branch.pack(pady=12, padx=10)

course_title = CTkComboBox(master=mark_attendance_frame, values=["Course Title","IPC","DCS","PGD","MOOC","QC","HCI","NNDL","NLP","AI"]) course_title.pack(pady=12, padx=10)

register_button1 = ctk.CTkButton(master=mark_attendance_frame, text='Mark Attendance') register_button1.pack(pady=12, padx=10)

This is the frame for the "View Attendance" tab

view_attendance_frame = tabview.tab("View Attendance")

semester = CTkComboBox(master=view_attendance_frame, values=["Semester","Semester 1", "Semester 2", "Semester 3","Semester 4","Semester 5","Semester 6", "Semester 7","Semester 8"]) semester.pack(pady=12, padx=10)

branch = CTkComboBox(master=view_attendance_frame, values=["Branch Name","CSE", "ECE"]) branch.pack(pady=12, padx=10)

course_title = CTkComboBox(master=view_attendance_frame, values=["Course Title","IPC","DCS","PGD","MOOC","QC","HCI","NNDL","NLP","AI"]) course_title.pack(pady=12, padx=10)

register_button1 = ctk.CTkButton(master=view_attendance_frame, text='View Attendance') register_button1.pack(pady=12, padx=10)

app.mainloop()

This is my current code for our desktop application of attendance management system in python using tkinter and customtkinter. I want to add the window which takes students data from the provided students details file. I want the radio buttons infront of each students entry for present and absent. After clicking on respective radio buttons it should add the result in seperate excel file. Also calculate the overall count of present and absent students. Write a code in python for above features and tell me where to add that code in my program.

To include the functionality you described, you'll need to implement a new window that allows you to select student data with radio buttons for marking attendance and then record the attendance in a separate Excel file.

This new functionality will include the following steps:

  1. Create a new window when the 'Mark Attendance' button is pressed.
  2. Load student data from a CSV file and display it in a list with radio buttons for present or absent.
  3. Collect the attendance data and save it to a new Excel file; also calculate the count of present and absent students.

Here is the Python code to achieve the functionality, with explanations on where to add it to your existing program:

Step 1: Define a new function to handle the mark attendance process. You need to add this function definition below the choose_file_and_update() function in your existing code:

import openpyxl # Make sure to install this package # Function to load students, mark attendance, and save the results def mark_attendance(): attendance_window = tk.Toplevel() # Create a new top-level window attendance_window.geometry("600x400") attendance_window.title("Mark Student Attendance") def save_attendance(): # Save the attendance to an Excel file wb = openpyxl.Workbook() sheet = wb.active sheet.title = 'Attendance' sheet.append(["Student Name", "Roll Number", "Attendance"]) present_count = 0 absent_count = 0 for entry in attendance_data: attendance = 'Present' if entry['attendance'].get() == 'P' else 'Absent' sheet.append([entry['name'], entry['roll_number'], attendance]) if attendance == 'Present': present_count += 1 else: absent_count += 1 # Add the counts to the end of the Excel file sheet.append([]) sheet.append(['Total Present', present_count]) sheet.append(['Total Absent', absent_count]) save_path = filedialog.asksaveasfilename(defaultextension=".xlsx", filetypes=[("Excel files", "*.xlsx")]) if save_path: wb.save(save_path) tkmb.showinfo("Success", "Attendance saved successfully") # Function to load students data into the window def load_students(): filepath = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv")]) if filepath: with open(filepath, newline='') as csvfile: reader = csv.DictReader(csvfile) for i, row in enumerate(reader, 1): row_frame = tk.Frame(attendance_window) row_frame.pack(fill='x') tk.Label(row_frame, text=row['Student Name']).pack(side='left') tk.Label(row_frame, text=row['Roll Number']).pack(side='left', padx=(10, 0)) attendance_var = tk.StringVar(value='A') # Default to absent present_rb = tk.Radiobutton(row_frame, text='Present', variable=attendance_var, value='P') present_rb.pack(side='left', padx=(10, 0)) absent_rb = tk.Radiobutton(row_frame, text='Absent', variable=attendance_var, value='A') absent_rb.pack(side='left', padx=(10, 0)) attendance_data.append({'name': row['Student Name'], 'roll_number': row['Roll Number'], 'attendance': attendance_var}) attendance_data = [] # Hold the attendance data as a list of dicts load_button = ctk.CTkButton(attendance_window, text='Load Students', command=load_students) load_button.pack(pady=12) save_button = ctk.CTkButton(attendance_window, text='Save Attendance', command=save_attendance) save_button.pack(pady=12) attendance_window.mainloop()

Step 2: Modify the 'Mark Attendance' button command. Locate the 'Mark Attendance' tab section in your code and change the register_button1 command option to command=mark_attendance:

register_button1 = ctk.CTkButton(master=mark_attendance_frame, text='Mark Attendance', command=mark_attendance) register_button1.pack(pady=12, padx=10)

Now, when you run your application and go to the 'Mark Attendance' tab, you should be able to load the student data from a CSV file and mark each student's attendance as present or absent.

Also note, you will need to install openpyxl if it's not already installed, as it's required to create Excel files:

pip install openpyxl

Keep in mind that attendance_data stores the necessary information within the mark_attendance function and allows you to save the data to an Excel file with a summary of attendance at the end.