Advertisement
ALENTL

main.py

Oct 30th, 2022
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.02 KB | None | 0 0
  1. from tkinter import messagebox
  2. import mysql.connector as mc
  3. from tkinter import *
  4.  
  5. # MySQL Objects and connection string
  6. cnx = mc.connect(host="localhost", user="alen", password="alen", database="school")
  7. cur = cnx.cursor()
  8. cnx.autocommit = True
  9.  
  10. """
  11. Home function is the default home page. It consists of the  login page. It asks the user which group he/she belongs to ie (student, teacher, staff, admin)
  12. """
  13. def home():
  14.     window = Tk()
  15.     window.title("SCHOOL MANAGEMENT SYSTEM")
  16.     window.geometry("960x606")
  17.     window.resizable(0,0)
  18.     window.iconbitmap("./ico/school.ico")
  19.  
  20.     # Creating background image variable
  21.     bg = PhotoImage(file="./img/login.png")
  22.  
  23.     # Creating the background label
  24.     l1 = Label(window, image=bg)
  25.     l1.place(x=0, y=0)
  26.  
  27.     # Creating the login text
  28.     l2 = Label(window, text="LOGIN", font=("times new roman", 20, "bold"), bg="#184800", fg="#ff007f")
  29.     l2.place(x=441, y=23)
  30.  
  31.     # Creating the student button
  32.     l3 = Button(window, text="STUDENT", width=15, font=("times new roman",10, "bold"), command=lambda: stud_lgn_btn(window))
  33.     l3.place(x=435, y=100)
  34.  
  35.     # Creating the teacher button
  36.     l4 = Button(window, text="TEACHER", width=15, font=("times new roman", 10, "bold"))
  37.     l4.place(x=435, y=140)
  38.  
  39.     # Creating the staff button
  40.     l5 = Button(window, text="STAFF", width=15, font=("times new roman", 10, "bold"))
  41.     l5.place(x=435, y=180)
  42.  
  43.     # Creating the admin button
  44.     l6 = Button(window, text="ADMIN", width=15, font=("times new roman",10, "bold"))
  45.     l6.place(x=435, y=220)
  46.  
  47.     window.mainloop()
  48.  
  49. def stud_lgn_btn(window):
  50.     # Killing the previous window and moving to the student window
  51.     window.destroy()
  52.  
  53.     # Making the tkinter window global
  54.     global root
  55.  
  56.     # Creating new window
  57.     root = Tk()
  58.  
  59.     root.title("STUDENT LOGIN")
  60.     root.geometry("960x600")
  61.     root.resizable(0,0)
  62.     root.iconbitmap("./ico/student.ico")
  63.  
  64.     #Creating String Vars
  65.     global username_var
  66.     global passw_var
  67.     username_var = StringVar()
  68.     passw_var = StringVar()
  69.    
  70.     # Creating the background image variable
  71.     bg = PhotoImage(file="./img/student_lgn.png")
  72.  
  73.     # Creating the label background
  74.     l1 = Label(root, image=bg)
  75.     l1.place(x=0, y=0)
  76.  
  77.     # Creating student login text
  78.     l2 = Label(root, text="STUDENT LOGIN", background="#413E91", font=("times new roman", 20, "bold"), fg="black")
  79.     l2.place(x=385, y=13)
  80.  
  81.     # Creating the login username text
  82.     lgnt1 = Label(root, text="USERNAME", font=("times new roman", 10, "bold"))
  83.     lgnt1.place(x=40, y=140)
  84.    
  85.     # Creating the username login entry form
  86.     global user
  87.     user = Entry(root, textvariable=username_var, width=20)
  88.     user.place(x=140, y=140)
  89.  
  90.     # Creating the login password text
  91.     lgnt2 = Label(root, text="PASSWORD", font=("times new roman", 10, "bold"))
  92.     lgnt2.place(x=40, y=180)
  93.  
  94.     # Creating the password entry form
  95.     pswd = Entry(root, textvariable=passw_var, width=20, show="*")
  96.     pswd.place(x=140, y=180)
  97.  
  98.     # Creating the submit button
  99.     submit = Button(root, text="SUBMIT", command=stud_lgn)
  100.     submit.place(x=130, y=220)
  101.  
  102.     root.mainloop()
  103.  
  104. def stud_lgn():
  105.     name = username_var.get()
  106.     passw = passw_var.get()
  107.  
  108.     cur.execute("SELECT * FROM STUDENTS WHERE USERNAME=%s AND PASSWORD=%s", (name, passw))
  109.  
  110.     a = cur.fetchall()
  111.  
  112.     if any(a):
  113.         root.destroy()
  114.         stud_home()
  115.    
  116.     else:
  117.         messagebox.showerror("User nor found", "Cannot find such a user with the given username and password. Please retry")
  118.  
  119.     username_var.set("")
  120.     passw_var.set("")
  121.  
  122. def stud_home():
  123.     # Creating the student home window
  124.     shw = Tk()
  125.     shw.title("STUDENT HOME")
  126.     shw.iconbitmap("./ico/student.ico")
  127.     shw.geometry("960x521")
  128.     shw.resizable(0,0)
  129.  
  130.     # Creating the background image variable
  131.     bg = PhotoImage("./img/stud_home.png")
  132.  
  133.     # Creating the label background
  134.     l1 = Label(shw, image=bg)
  135.     l1.pack()
  136.  
  137. if __name__ == "__main__":
  138.     home()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement