daily pastebin goal
61%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import tkinter as tk
  2. from tkinter import messagebox
  3. import csv
  4. import operator
  5.  
  6. LARGE_FONT= ("Verdana", 12)
  7.  
  8. class main(tk.Tk):
  9.     def __init__(self, *args, **kwargs):
  10.         tk.Tk.__init__(self, *args, **kwargs)
  11.         container = tk.Frame(self)
  12.         container.pack(side="top", fill="both", expand = True)
  13.         container.grid_rowconfigure(0, weight=1)
  14.         container.grid_columnconfigure(0, weight=1)
  15.         self.geometry("1500x600")
  16.         self.frames = {}
  17.         for F in (auth, menu, addStudent, getStudent, reports):
  18.             frame = F(container, self)
  19.             self.frames[F] = frame
  20.             frame.grid(row=0, column=0, sticky="nsew")
  21.         self.show_frame(auth)
  22.  
  23.     def show_frame(self, cont):
  24.         frame = self.frames[cont]
  25.         frame.tkraise()
  26.  
  27. class auth(tk.Frame):
  28.     failedLogins = 0
  29.     def __init__(self, parent, controller):
  30.         tk.Frame.__init__(self,parent)
  31.         label = tk.Label(self, text="Auth", font=LARGE_FONT)
  32.         label.pack(pady=10,padx=10)
  33.  
  34.         usernameEntry = tk.Entry(self)
  35.         passwordEntry = tk.Entry(self, show="*")
  36.         usernameLabel = tk.Label(self, text="Username").pack()
  37.         usernameEntry.pack()
  38.         pas1swordLabel = tk.Label(self, text="Password").pack()
  39.         passwordEntry.pack()
  40.  
  41.         def login_btn_clicked():
  42.             #print("Clicked")
  43.             username = usernameEntry.get()
  44.             password = passwordEntry.get()
  45.            
  46.             if username == "leeman" and password == "password":
  47.                 print("Done Auth")
  48.                 controller.show_frame(menu)
  49.             elif self.failedLogins >= 3:
  50.                 messagebox.showerror("Login failed", "Too many incorrect login attempts")
  51.             else:
  52.                 self.failedLogins+=1
  53.        
  54.         button = tk.Button(self, text="Login", command=login_btn_clicked)
  55.         button.pack()
  56.  
  57. class menu(tk.Frame):
  58.     def __init__(self, parent, controller):
  59.         tk.Frame.__init__(self, parent)
  60.         label = tk.Label(self, text="Main menu", font=LARGE_FONT)
  61.         label.pack(pady=10,padx=10)
  62.  
  63.         button1 = tk.Button(self, text="Add student", command=lambda: controller.show_frame(addStudent))
  64.         button1.pack()
  65.         button2 = tk.Button(self, text="Get student", command=lambda: controller.show_frame(getStudent))
  66.         button2.pack()
  67.         button3 = tk.Button(self, text="Get reports", command=lambda: controller.show_frame(reports))
  68.         button3.pack()
  69.         button4 = tk.Button(self, text="Log out", command=lambda: controller.show_frame(auth))
  70.         button4.pack()
  71.  
  72.  
  73. class addStudent(tk.Frame):
  74.     def __init__(self, parent, controller):
  75.         tk.Frame.__init__(self, parent)
  76.         label = tk.Label(self, text="Add student", font=LARGE_FONT)
  77.         label.pack(pady=10,padx=10)
  78.  
  79.         button1 = tk.Button(self, text="Home",
  80.                             command=lambda: controller.show_frame(menu))
  81.         button1.pack()
  82.        
  83.         def newID():
  84.             with open('tutGroup.csv') as csvfile:
  85.                 allID = []
  86.                 spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
  87.                 for row in spamreader:
  88.                     allID.append(row)
  89.                 newID = (len(allID))+1119
  90.                 return newID
  91.                
  92.         def processStudent():
  93.             forename = forenameEntry.get()
  94.             surname  = surnameEntry.get()
  95.             dob      = DOBEntry.get()
  96.             address  = addressEntry.get()
  97.             phone    = phoneEntry.get()
  98.             gender   = genderEntry.get()
  99.             email    = str(surname+"."+forename+"@treeschhol.com")
  100.  
  101.             with open('tutGroup.csv', 'a') as csvfile:
  102.                 writer=csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
  103.                 writer.writerow([newID(),surname,forename,dob,address,phone,gender,'L15',email])
  104.                 messagebox.showinfo("Student Added", "This student has been added to the tutor group")
  105.  
  106.            
  107.        
  108.         forenameEntry = tk.Entry(self)
  109.         forenameLabel = tk.Label(self, text="Forename").pack()
  110.         forenameEntry.pack()
  111.         surnameEntry = tk.Entry(self)
  112.         surnameLabel = tk.Label(self, text="Surename").pack()
  113.         surnameEntry.pack()
  114.         DOBEntry = tk.Entry(self)
  115.         DOBLabel = tk.Label(self, text="Date of Birth (DD/MM/YYYY)").pack()
  116.         DOBEntry.pack()
  117.         addressEntry = tk.Entry(self)
  118.         addressLabel = tk.Label(self, text="Home address").pack()
  119.         addressEntry.pack()
  120.         phoneEntry = tk.Entry(self)
  121.         phoneLabel = tk.Label(self, text="Phone number").pack()
  122.         phoneEntry.pack()
  123.         genderEntry = tk.Entry(self)
  124.         genderLabel = tk.Label(self, text="Gender").pack()
  125.         genderEntry.pack()
  126.  
  127.         button2 = tk.Button(self, text="Submit", command=processStudent)
  128.         button2.pack()
  129.        
  130. class getStudent(tk.Frame):
  131.     def __init__(self, parent, controller):
  132.         tk.Frame.__init__(self, parent)
  133.         label = tk.Label(self, text="Get student", font=LARGE_FONT)
  134.         label.pack(pady=10,padx=10)
  135.  
  136.         button1 = tk.Button(self, text="Back to Home",
  137.                             command=lambda: controller.show_frame(menu))
  138.         button1.pack()
  139.  
  140.         def showStudent():
  141.             unqID = idEntry.get()
  142.             allStud = []
  143.             i = 0
  144.             with open('tutGroup.csv', 'r') as csvfile:
  145.                 spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
  146.                 for row in spamreader:
  147.                     allStud.append(row)
  148.            
  149.             while i < len(allStud):
  150.                 ID = allStud[i][0]
  151.                 if ID == unqID:
  152.                     forename.set(allStud[i][2])
  153.                     surname.set(allStud[i][1])
  154.                     email.set(allStud[i][8])
  155.                     dob.set(allStud[i][3])
  156.                     address.set(allStud[i][4].replace('"', ''))
  157.                     phone.set(allStud[i][5])
  158.                     gender.set(allStud[i][6])
  159.                 i += 1
  160.  
  161.         idEntry = tk.Entry(self)
  162.         idLabel = tk.Label(self, text="Student ID").pack()
  163.         idEntry.pack()
  164.         button2 = tk.Button(self, text="Submit", command=showStudent)
  165.         button2.pack()
  166.  
  167.         forename = tk.StringVar()
  168.         surname = tk.StringVar()
  169.         email = tk.StringVar()
  170.         dob = tk.StringVar()
  171.         address = tk.StringVar()
  172.         phone = tk.StringVar()
  173.         gender = tk.StringVar()
  174.  
  175.         forenameEntry = tk.Entry(self, textvariable=forename)
  176.         forenameLabel = tk.Label(self, text="Forename").pack()
  177.         forenameEntry.pack()
  178.         surnameEntry = tk.Entry(self, textvariable=surname)
  179.         surnameLabel = tk.Label(self, text="Surename").pack()
  180.         surnameEntry.pack()
  181.         emailEntry = tk.Entry(self, textvariable=email)
  182.         emailLabel = tk.Label(self, text="Email").pack()
  183.         emailEntry.pack()
  184.         DOBEntry = tk.Entry(self, textvariable=dob)
  185.         DOBLabel = tk.Label(self, text="Date of Birth (DD/MM/YYYY)").pack()
  186.         DOBEntry.pack()
  187.         addressEntry = tk.Entry(self, textvariable=address)
  188.         addressLabel = tk.Label(self, text="Home address").pack()
  189.         addressEntry.pack()
  190.         phoneEntry = tk.Entry(self, textvariable=phone)
  191.         phoneLabel = tk.Label(self, text="Phone number").pack()
  192.         phoneEntry.pack()
  193.         genderEntry = tk.Entry(self, textvariable=gender)
  194.         genderLabel = tk.Label(self, text="Gender").pack()
  195.         genderEntry.pack()
  196.  
  197. class reports(tk.Frame):
  198.     def __init__(self, parent, controller):
  199.         tk.Frame.__init__(self, parent)
  200.         label = tk.Label(self, text="Reports", font=LARGE_FONT)
  201.         label.pack(pady=10,padx=10)
  202.  
  203.         def set_text(text):
  204.             results.delete("1.0", tk.END)
  205.             for row in text:
  206.                 resultString = row['ID'],row['forename'],row['surname'].upper(),row['dob'],row['home address'],row['phone'],row['email'],row['gender'].upper()
  207.                 emptyString = "\n\n"
  208.                 results.insert("1.0", resultString)
  209.                 results.insert("1.0", emptyString)
  210.             return
  211.  
  212.         def reportsID():
  213.             with open('tutGroup.csv') as csvfile:
  214.                 spamreader = csv.DictReader(csvfile, delimiter=",")
  215.                 sortedlist = sorted(spamreader, key=lambda row:(row['ID']), reverse=True)
  216.                 set_text(sortedlist)
  217.                
  218.         def reportsGender():
  219.             with open('tutGroup.csv') as csvfile:
  220.                 spamreader = csv.DictReader(csvfile, delimiter=",")
  221.                 sortedlist = sorted(spamreader, key=lambda row:(row['gender']), reverse=True)
  222.                 set_text(sortedlist)
  223.                
  224.         def reportsAlpha():
  225.             with open('tutGroup.csv') as csvfile:
  226.                 spamreader = csv.DictReader(csvfile, delimiter=",")
  227.                 sortedlist = sorted(spamreader, key=lambda row:(row['surname']), reverse=True)
  228.                 set_text(sortedlist)
  229.    
  230.         button1 = tk.Button(self, text="Back to Home",
  231.                             command=lambda: controller.show_frame(menu))
  232.         button1.pack()
  233.         button2 = tk.Button(self, text="Sort students by ID", command=lambda: reportsID())
  234.         button2.pack()
  235.         button3 = tk.Button(self, text="Sort students by Gender", command=lambda: reportsGender())
  236.         button3.pack()
  237.         button4 = tk.Button(self, text="Sort students Alphabetically", command=lambda: reportsAlpha())
  238.         button4.pack()
  239.  
  240.         results = tk.Text(self, width=1920, height=1080)
  241.         results.pack()
  242.        
  243. app = main()
  244. app.mainloop()
RAW Paste Data
Top