#Daniel R. Spohn #Imports Tkinter for gui creation from Tkinter import * #Imports GradeDB from group_proj import GradeDB #Declares a class named Application which is the gui interface class Application(Frame): #The constructor which loads when a new instance of the class is created def __init__(self, master): Frame.__init__(self,master) self.grid() self.create_widgets() #This method, displays the "widgets" on the form def create_widgets(self): #creates and displays a label Label(self, text = "Please Select An Option" ).grid(row = 0,column = 0, sticky = W) #declares some variables self.menu_option = StringVar() self.menu_options = StringVar() self.option_selected = "" self.student_name = "" self.student_id = "" self.assign = "" self.points = 0.0 #Creates radio buttons that can be selected to choose the options menu_options = ["Add Student", "Add Grade", "Grade Student", "Change Grade","Delete Student","Print Roster","Student Information","Exit"] row = 1 for part in menu_options: Radiobutton(self, text = part, variable = self.menu_option, value = row ).grid(row = row, column = 0, sticky = W) row += 1 #Creates a button used to select an option self.bttnMakeSelection = Button(self, width = 15, text = "Choose Option",command= self.choose_option) self.bttnMakeSelection.grid(row = 9, column = 0, sticky = W) #Creates a text box that is used to display useful information self.information_txt = Text(self, width = 60, height = 15, wrap = WORD) self.information_txt.grid(row =10, column = 0, columnspan = 20,rowspan = 20, sticky = W) #Creates another label that is used to explain an input textbox Label(self, textvariable = v ).grid(row = 1, column = 17, sticky = W) ##Occurs when the user presses the button labled"Choose Option" ##Which every radio button is checked when the button is pressed selects that option def choose_option(self): menu_option = self.menu_option.get() self.option_selected = menu_option self.bttnMakeSelection.grid_remove() #Disables the Choose Option button so that it can not be pressed until an operation is complete self.bttnMakeSelection = Button(self, width = 15, text = "Choose Option",state = "disabled" ,command= self.choose_option) self.bttnMakeSelection.grid(row = 9, column = 0, sticky = W) #Determines which radio button is pressed if menu_option == "1": self.add_student() elif menu_option == "2": self.add_grade() elif menu_option == "3": self.grade_student() elif menu_option == "4": self.change_grade() elif menu_option == "5": self.delete_student() elif menu_option == "6": self.print_roster() elif menu_option == "7": self.student_info() elif menu_option == "8": MyDB.store() root.quit() root.destroy() else: self.finish_option() #This adds a student def add_student(self): self.information_txt.delete(0.0, END) self.print_roster() self.bttnMakeSelection.grid_remove() self.bttnMakeSelection = Button(self, width = 15, text = "Choose Option",state = "disabled" ,command= self.choose_option) self.bttnMakeSelection.grid(row = 9, column = 0, sticky = W) self.information_txt.insert(0.0, "Add Student\n") self.show_check_id() #This adds an assignment to the database def add_grade(self): self.information_txt.delete(0.0, END) self.show_check_assign() self.information_txt.insert(0.0, "Add Grade") #This allows the user to add a grade for a student def grade_student(self): self.information_txt.delete(0.0, END) self.print_roster() self.bttnMakeSelection.grid_remove() self.bttnMakeSelection = Button(self, width = 15, text = "Choose Option",state = "disabled" ,command= self.choose_option) self.bttnMakeSelection.grid(row = 9, column = 0, sticky = W) self.information_txt.insert(0.0, "Grade Student\n") self.show_check_id() #This allows the user to change a grade to the student, if a grade already exists def change_grade(self): self.information_txt.delete(0.0, END) self.print_roster() self.bttnMakeSelection.grid_remove() self.bttnMakeSelection = Button(self, width = 15, text = "Choose Option",state = "disabled" ,command= self.choose_option) self.bttnMakeSelection.grid(row = 9, column = 0, sticky = W) self.information_txt.insert(0.0, "Change Grade\n") self.show_check_id() #This allows a user to delete a student that is in the database def delete_student(self): self.information_txt.delete(0.0, END) self.print_roster() self.bttnMakeSelection.grid_remove() self.bttnMakeSelection = Button(self, width = 15, text = "Choose Option",state = "disabled" ,command= self.choose_option) self.bttnMakeSelection.grid(row = 9, column = 0, sticky = W) self.information_txt.insert(0.0, "Delete Student\n") self.show_check_id() #This will print out a roster into the text box def print_roster(self): roster = MyDB.roster() self.information_txt.delete(0.0, END) id = "" if(roster == False) : self.information_txt.insert(0.0, "Roster Not Found") else: for student in roster : name = student[0] id = student[1] name_id = name +"\t"+ str(id)+"\n" self.information_txt.insert(0.0 ,name_id) self.finish_option() #This will show info about a student def show_info(self): self.information_txt.delete(0.0, END) info = MyDB.studentinfo(self.student_id) if( info == False ) : self.information_txt.insert(0.0, "Error occured!") else : self.information_txt.insert(0.0, info) self.bttn_id.grid_remove() self.finish_option() #Begins the process of showing a student's info def student_info(self): self.information_txt.delete(0.0, END) self.print_roster() self.bttnMakeSelection.grid_remove() self.bttnMakeSelection = Button(self, width = 15, text = "Choose Option",state = "disabled" ,command= self.choose_option) self.bttnMakeSelection.grid(row = 9, column = 0, sticky = W) self.show_check_id() #Shows the textbox label and button for inputing student id def show_check_id(self): self.bttn_id = Button(self, width = 5, height = 1, text = "Ok",command = self.check_id) self.bttn_id.grid(row = 1, column = 19 , sticky = W) v.set("Enter Student ID:") self.id_ent = Entry(self) self.id_ent.grid(row = 1, column = 18, sticky = W) #Checks an ID, If no ID is entered then will exit the operation. After ID is entered, it will decide which option was chosen from the menu def check_id(self): if self.student_id == "": if self.id_ent.get() == "": self.information_txt.delete(0.0, END) self.information_txt.insert(0.0, "No ID was entered") self.id_ent.grid_remove() self.bttn_id.grid_remove() self.finish_option() else: try: self.student_id = self.id_ent.get() int(self.student_id) except(ValueError): self.information_txt.delete(0.0, END) self.information_txt.insert(0.0, "Must enter number for student id!!") self.bttn_id.grid_remove() self.id_ent.grid_remove() self.finish_option() else: self.id_ent.grid_remove() if self.option_selected == "1": v.set("Enter Student Name:") self.name_ent = Entry(self) self.name_ent.grid(row = 1, column = 18, sticky = W) elif self.option_selected == "3" or self.option_selected == "4": v.set("Enter Test Name:") self.name_ent = Entry(self) self.name_ent.grid(row = 1, column = 18, sticky = W) elif self.option_selected == "5": self.finish_delete_student() elif self.option_selected == "7": self.show_info() else: if self.option_selected == "1": self.student_name = self.name_ent.get() self.finish_add_student() elif self.option_selected == "3" or self.option_selected == "4": if self.name_ent.get() == "": self.information_txt.delete(0.0, END) self.information_txt.insert(0.0, "No Name was entered") self.name_ent.grid_remove() self.finish_option() self.bttn_id.grid_remove() else: self.assign = self.name_ent.get() self.name_ent.grid_remove() v.set("Grade Given:") self.bttn_id.grid_remove() self.score_ent = Entry(self) self.score_ent.grid(row = 1, column = 18, sticky = W) if self.option_selected == "3": self.bttn_id = Button(self, width = 5, height = 1, text = "Ok",command = self.finish_grade_student) else:self.bttn_id = Button(self, width = 5, height = 1, text = "Ok",command = self.finish_change_grade) self.bttn_id.grid(row = 1, column = 19 , sticky = W) #Finishes the change grade process def finish_change_grade(self): self.information_txt.delete(0.0, END) try: self.points = self.score_ent.get() int(self.points) except(ValueError): self.information_txt.insert(0.0, "Must enter number for score!!") else: if not MyDB.changeofgrade(self.student_id,self.assign,self.points) : self.information_txt.insert(0.0, "Error occured!\nGrade not changed!") else : info = MyDB.studentinfo(self.student_id) self.information_txt.insert(0.0,"Grade for "+self.assign+ "changed to " + str(self.points)+ " for "+info['name']) self.score_ent.grid_remove() self.bttn_id.grid_remove() self.finish_option() # Finishes the grade student process def finish_grade_student(self): self.information_txt.delete(0.0, END) try: self.points = self.score_ent.get() int(self.points) except(ValueError): self.information_txt.insert(0.0, "Must enter number for score!!") else: if( not MyDB.gradestudent(self.student_id,self.assign,self.points) ) : self.information_txt.insert(0.0, "Grading of student failed!") else : info = MyDB.studentinfo(self.student_id) self.information_txt.insert(0.0,info['name']) self.information_txt.insert(0.0, "Grade " + str(self.points) + " for " + self.assign +" given to ") self.score_ent.grid_remove() self.bttn_id.grid_remove() self.finish_option() #Adds buttons for adding an assignment def show_check_assign(self): self.bttn_assign = Button(self, width = 5, height = 1, text = "Ok",command = self.check_assign) self.bttn_assign.grid(row = 1, column = 19 , sticky = W) v.set("Enter Assignment Name:") self.assign_ent = Entry(self) self.assign_ent.grid(row = 1, column = 18, sticky = W) #Gets the assignement name, and the grade that it is out of and adds them to the database def check_assign(self): if self.assign == "": if self.assign_ent.get() == "": self.information_txt.delete(0.0, END) self.information_txt.insert(0.0, "No Assignment Name was entered") self.assign_ent.grid_remove() self.bttn_assign.grid_remove() self.finish_option() else: self.assign = self.assign_ent.get() self.assign_ent.grid_remove() v.set("Number of points this is out of:") self.points_ent = Entry(self) self.points_ent.grid(row = 1, column = 18, sticky = W) else: self.information_txt.delete(0.0, END) try: self.points = self.points_ent.get() int(self.points) except(ValueError): self.information_txt.insert(0.0, "Must enter number for score!!") else: if(not MyDB.addgrade(self.assign,self.points)) :self.information_txt.insert(0.0, "Grade adding failed!") else : self.information_txt.insert(0.0, "Grade "+ self.assign + " out of " + str(self.points) +"points was added") self.assign = "" self.points = "" self.bttn_assign.grid_remove() self.points_ent.grid_remove() self.finish_option() #Finishes add student def finish_add_student(self): self.information_txt.delete(0.0, END) if(not MyDB.addstudent(self.student_name,self.student_id)): self.information_txt.insert(0.0, "Student wasn't added!") else : self.information_txt.insert(0.0, "Student added!") self.bttn_id.grid_remove() self.name_ent.grid_remove() self.finish_option() #Finished delete student def finish_delete_student(self): self.information_txt.delete(0.0, END) if not MyDB.deletestudent(self.student_id) : self.information_txt.insert(0.0,"Delete of student failed!") else : self.information_txt.insert(0.0,"Deleted ID number: " + str(self.student_id)) self.bttn_id.grid_remove() self.finish_option() #This is run after each operation is complete, to clear out values and reset the form def finish_option(self): self.student_name = "" self.student_id = "" self.assign = "" self.points = "" v.set("") self.bttnMakeSelection.grid_remove() self.bttnMakeSelection = Button(self, width = 15, text = "Choose Option",state = "normal",command= self.choose_option) self.bttnMakeSelection.grid(row = 9, column = 0, sticky = W) #Declares root as a gui form root = Tk() root.title("Student Database") root.geometry("440x500") v = StringVar() MyDB = GradeDB() app = Application(root) app.grid #Keeps the form open root.mainloop()