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

import customtkinter as ctk from customtkinter import * import tkinter as tk from tkinter import filedialog, Toplevel, Radiobutton, Label, Button 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")

def open_attedance_window(): ctk.set_appearance_mode("dark") ctk.set_default_color_theme("blue") attendance_app = ctk.CTkToplevel() attendance_app.geometry("500x550") attendance_app.title("ATTENDO")

attend_semester=mark_attendance_semester.get() attend_branch=mark_attendance_branch.get() attend_course=mark_attendance_course_title.get() with open('students.csv', mode='r', newline='') as file: reader = csv.reader(file) next(reader) for i, row in enumerate(reader): if row[2] == attend_semester and row[3] == attend_branch and row[4] == attend_course: attend_label = ctk.CTkLabel(master=attendance_app,text=row[0]).grid(row=i,column=0,ipadx=10,ipady=10) attend_label = ctk.CTkLabel(master=attendance_app,text=row[1]).grid(row=i,column=1,ipadx=10,ipady=10) present_var = ctk.StringVar(value="present") CTkRadioButton(master=attendance_app,variable=present_var, text="PRESENT",value="present").grid(row=i,column=2) CTkRadioButton(master=attendance_app,variable=present_var, text="ABSENT",value="absent").grid(row=i,column=3)

ctk.set_appearance_mode("dark") ctk.set_default_color_theme("blue") app = ctk.CTk() app.geometry("500x550") 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)

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

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

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

This is the frame for the "Mark Attendance" tab

mark_attendance_frame = tabview.tab("Mark Attendance")

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"]) mark_attendance_semester.pack(pady=12, padx=10)

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

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

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

This is the frame for the "View Attendance" tab

view_attendance_frame = tabview.tab("View Attendance")

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"]) view_attendance_semester.pack(pady=12, padx=10)

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

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

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

app.mainloop()

This is my current code for desktop application of attendance management system in python using tkinter and customtkinter, I want one button saying "Submit Attendance" at the bottom of window in def open_attedance_window() this part of code. This button should be fully functional. Write a code for adding that button in suggested place and tell me where to add that code

To add a "Submit Attendance" button at the bottom of the open_attedance_window window your attendance management system, you'll need to create a new ctk.CTkButton widget and place it correctly using the grid manager inside the open_attedance_window function. Also, you will need to create a callback function that will be called when the button is pressed to handle the submission of attendance.

First, write the callback function that should be called when the "Submit Attendance" button is pressed. For this example, this function will display a message box stating that attendance has been submitted. You will need to add your own logic to handle the actual submission.

Here is the function you can add just above your open_attedance function:

def submit_attendance(): # Your logic to submit attendance goes here tkmb.showinfo("Submit Attendance", "Attendance has been submitted successfully")

Now, add the following code at the end of your open_ance_window function:

# Creating the "Submit Attendance" button submit_attendance_button = ctk.CTkButton(master=attendance_app,='Submit Attendance', command=submit_attendance) submit_attendance_button.grid(row=i+1, columnspan=4, pady=20)

Make sure you place it after the loop that sets up the attendance checkboxes so that it appears at the end of the form. Your updated open_attedance_window function will look like this with the button added:

def open_attedance_window(): ctk.set_appearance_mode("dark") ... with open('students.csv', mode='r', newline='') as file: reader = csv.reader(file) next(reader) for i, row in enumerate(reader): if row[2 == attend_semester and row[3] == attend_branch and row[4] attend_course: ... present_var = ctk.StringVar(valuepresent") CTkRadioButton(master=attendance_app,variable=present_var, text="PRESENT",value="present").grid(row=i,column=2) CTk(master=attendance_app,variable=present_var, text="ABSENT",value="absent").grid(row=i,column=3) # Creating the "Submit Attendance" button submit_attendance_button = ctk.CTkButton(master=attendance_app, text='Submit Attendance', command=submit_attendance) submit_attendance_button.grid(row=i+1, columnspan=4, pady=20)

This new button will now appear at the bottom of the attendance window, and when clicked, will trigger the submit_attendance function Please ensure to implement the logic within the submit_attendance function to actually save or process the attendance data to your application's requirements.

import customtkinter as ctk from customtkinter import * import tkinter as tk from tkinter import filedialog, Toplevel, Radiobutton, Label, Button 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")

def submit_attendance(): # Your logic to submit attendance goes here tkmb.showinfo("Submit Attendance", "Attendance has been submitted successfully")

def open_attedance_window(): ctk.set_appearance_mode("dark") ctk.set_default_color_theme("blue") attendance_app = ctk.CTkToplevel() attendance_app.geometry("500x550") attendance_app.title("ATTENDO")

attend_semester=mark_attendance_semester.get() attend_branch=mark_attendance_branch.get() attend_course=mark_attendance_course_title.get() with open('students.csv', mode='r', newline='') as file: reader = csv.reader(file) next(reader) for i, row in enumerate(reader): if row[2] == attend_semester and row[3] == attend_branch and row[4] == attend_course: attend_label = ctk.CTkLabel(master=attendance_app,text=row[0]).grid(row=i,column=0,ipadx=10,ipady=10) attend_label = ctk.CTkLabel(master=attendance_app,text=row[1]).grid(row=i,column=1,ipadx=10,ipady=10) present_var = ctk.StringVar(value="present") CTkRadioButton(master=attendance_app,variable=present_var, text="PRESENT",value="present").grid(row=i,column=2) CTkRadioButton(master=attendance_app,variable=present_var, text="ABSENT",value="absent").grid(row=i,column=3) # Creating the "Submit Attendance" button submit_attendance_button = ctk.CTkButton(master=attendance_app, text='Submit Attendance', command=submit_attendance) submit_attendance_button.grid(row=i+1, columnspan=4, pady=20)

ctk.set_appearance_mode("dark") ctk.set_default_color_theme("blue") app = ctk.CTk() app.geometry("500x550") 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)

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

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

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

This is the frame for the "Mark Attendance" tab

mark_attendance_frame = tabview.tab("Mark Attendance")

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"]) mark_attendance_semester.pack(pady=12, padx=10)

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

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

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

This is the frame for the "View Attendance" tab

view_attendance_frame = tabview.tab("View Attendance")

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"]) view_attendance_semester.pack(pady=12, padx=10)

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

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

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

app.mainloop()

This is my current code for desktop application of attendance management system in python using tkinter and customtkinter, i want to show the submitted attendance which is done after clicking the "Submit Attendance" Button inside def open_attedance_window() this part of code in csv file. This csv file should display when i click on "View Attendance" Button in "View Attendance" tab. Write a code for the above suggested feature and tell me where to add the code in my program.

To implement the feature where you can view submitted attendance when you click on the "View Attendance" button, you will need to do the following:

  1. Modify the submit_attendance function to collect the attendance data and write it to a CSV file.
  2. Modify the View Attendance tab to add functionality to the "View Attendance" button, which will open the CSV file containing the attendance and display it.

First, let's modify the `submit_attendance function. This example assumes that you'll collect "PRESENT" or "ABSENT" data for each, but you'll need to modify this according to the actual data structure of your application.

def submit_attendance(): # Assuming you have a to collect attendance data attendance_data = collect_attendance_data() # You need write this function with open('attendance.csv', mode='w', newline='') as file: writer = csv.writer(file) writer.writerows(attendance_data) tkmb.showinfo("Submit Attendance", "Attendance has been submitted successfully") def collect_attendance_data(): # Replace with your method to retrieve attendance from the attendance window # This is just an example of what the data might look like return [['Name', 'Roll No.', 'Attendance'], ['John Doe', '001', 'PRESENT'], [' Smith', '002', 'ABSENT']]

Second for the View Attendance tab, you need to define a function that the "View Attendance" button will call to display the attendance CSV file.

def view_attendance(): try: # This checks if the CSV file exists and can be read with open('attendance.csv', mode='r', newline as file: # Creating a new top-level window to show the contents of the attendance file attendance_window = Toplevel(app) attendance_window.title("Attendance Records") # Adding a text widget to display the contents of the CSV text_widget = tk.Text(attendance_window) text_widget.pack(padx=10, pady=10, fill='both', expand=True) # Reading the CSV and inserting it into the text widget attendance_reader = csv.reader(file) for row in attendance_reader: text_widget.insert('end', ','.join(row) + '\n') except IOError: tkmb.showerror("Error", "Attendance file not found or could not be opened.") # Modify the current 'View Attendance' button command view_attendance_button.config(command=view_attendance)

Now add the view_attendance function to your code, and the "View Attendance" button to this function using the config method as shown.

Make sure to replace the placeholder collect_attendance_data function with an actual to collect attendance data from the user interface. You'll need to collect each student's attendance status and put it in a list of lists where each sublist represents a row in the CSV file.

Add these function blocks to your code where you're defining your functions, before initiating app.mainloop().

Keep in mind that error handling is very important. The provided example for viewing attendance data assumes the 'attendance' file exists and is correctly formatted. If the file is not found, or there's another error reading it, error message will be shown instead.