Demo entry 6785910

notice manager

   

Submitted by Zach on Mar 21, 2019 at 11:35
Language: Python 3. Code size: 16.2 kB.

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo
import mysql.connector
import _tkinter
config = {
  'user': 'root',
  'password': 'Zoozoo99',
  'host': '127.0.0.1',
  'database': 'mydb',
  'raise_on_warnings': True
}
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor(buffered=True)
LARGE_FONT = ("Verdana", 14)
global B
B=0
class Notice:
    def __init__(self,initials):
        global entry_username
        self.pupilids=("SELECT idpupil, name FROM pupil Where Hm_idhm ='"+initials+"'")
    def pull(self):
        cursor.execute(self.pupilids)
        global pupilids
        pupilids = cursor.fetchall()
        print(pupilids)

class display(tk.Tk,):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.iconbitmap(self, default='mc_ico_Z9D_icon.ico')
        tk.Tk.wm_title(self, "Notice Board")
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (menu, login, register, pupil_entry ,startpage, notice1, notice2):
            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(menu)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
def popup_bonus():
    v=[]
    global pupilids
    global B
    win = tk.Toplevel()
    win.wm_title("Window")
    cursor.execute("SELECT NoticeDescription, Urgency, Archived from Notice Where Pupil_idpupil = '"+str(pupilids[B][0])+"' ")
    data = cursor.fetchall()
    print(data)
    for l in range(len(data)):
        if data[l][1] != 0 and data[l][2] == '0':
            with open(r"C:\Users\ZCrea\OneDrive\CS\Noticefiles\\" + data[l][0]) as f:
                mylist = list(f)
                t = mylist[0]
                for z in range(5):
                    if data[l][1]==1:
                        t=("blue chit:"+mylist[0])
                    if data[l][1]==2:
                        t=("pink chit:"+mylist[0])
                    if data[l][1]==3:
                        t=("horror day or ban:"+mylist[0])
                    if data[l][1]==4:
                        t=("caution:"+mylist[0])
                    if data[l][1]==5:
                        t=("suspended:"+mylist[0])
                v.append(t)
        for i in range(len(v)):
            l = tk.Label(win, text=v[i])
            l.grid(row=i, column=0)

    b = ttk.Button(win, text="Okay", command=win.destroy)
    b.grid(row=0, column=2)
def popup_bonus2():
    v=[]
    global pupilids
    global B
    win = tk.Toplevel()
    win.wm_title("Window")
    cursor.execute("SELECT NoticeDescription, Urgency, Archived from Notice Where Pupil_idpupil = '"+str(pupilids[B][0])+"'")
    data = cursor.fetchall()
    for l in range(len(data)):
        if data[l][1] == 0 and data[l][2] == '0' :
            with open(r"C:\Users\ZCrea\OneDrive\CS\Noticefiles\\" + data[l][0]) as f:
                mylist = list(f)
                t = ("merit: "+mylist[0])
                v.append(t)
        for i in range(len(v)):
            l = tk.Label(win, text=v[i])
            l.grid(row=i, column=0)

    b = ttk.Button(win, text="Okay", command=win.destroy)
    b.grid(row=0, column=2)

def retry():
    showinfo("Window", "passwords do not match please try again")
def archive():
    global pupilids
    global B
    cursor.execute("SELECT IdNotice, Archived from Notice Where Pupil_idpupil = '" + str(pupilids[B][0]) + "'")
    archived=cursor.fetchall()
    for v in range(len(archived)):
        cursor.execute("UPDATE notice SET Archived= '1' Where Pupil_idpupil = '" + str(pupilids[B][0]) + "'")
    cnx.commit()

def popup_showinfo():
    global pupilids
    global B
    Pupil_name = ("SELECT Name, house, hm_idhm from Pupil Where idpupil ='"+str(pupilids[B][0])+"'")
    cursor.execute(Pupil_name)
    v=cursor.fetchall()
    print(v)
    showinfo("Window", v )
def forward():
    global B
    B=B+1
def back():
    global B
    B=B-1
def start():
    global B
    B=0
class menu(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = ttk.Label(self, text="Welcome To Your personal notice board", font=LARGE_FONT)
        label.pack(pady=10, padx=10)
        logins = ttk.Button(self, text="Login",
                           command=lambda: controller.show_frame(login))
        logins.pack()
        registers= ttk.Button(self, text="Register", command =lambda: (controller.show_frame(register)))
        registers.pack()
        exit = ttk.Button(self, text="would you like to exit", command=quit)
        exit.pack()
class login(tk.Frame):
  def __init__(self, parent, controller):
    global entry_username
    global entry_password
    tk.Frame.__init__(self, parent)
    label = ttk.Label(self, text="Please login so we can show you your notices", font=LARGE_FONT)
    label.pack(pady=10, padx=10)
    email = ttk.Label(self, text="Email")
    email.pack()
    entry_username = ttk.Entry(self)
    entry_username.pack()
    password = ttk.Label(self, text="password")
    password.pack()
    entry_password=ttk.Entry(self, show='*')
    entry_password.pack()
    showpass = ttk.Button(self, text="show password",
                          command=lambda: entry_password.config(show=""))
    showpass.pack()
    hidepass = ttk.Button(self, text="hide password",
                          command=lambda: entry_password.config(show="*"))
    hidepass.pack()
    Button=ttk.Button(self, text="Login",
                      command=lambda: self.checker(parent, controller))
    Button.pack()
    goback = ttk.Button(self, text="Go back", command =lambda: controller.show_frame(menu))
    goback.pack()
  def checker(self, parent, controller):
    print("Clicked")
    username = entry_username.get()
    password = entry_password.get()
    print(username, password)
    cursor.execute("SELECT Username, Password FROM HM_login where Username = '" + username + "'")
    password_verify = cursor.fetchall()
    print(password_verify[0][1])
    if password_verify[0][1] == password:
      controller.show_frame(startpage)
class register(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        global new_username
        # global new_password
        # global new_passwordverify
        # global new_email
        global Houseinitial
        label = ttk.Label(self,
                          text="if you would like to register please enter the correct details",
                          font=LARGE_FONT)
        label.pack(pady=10, padx=10)
        label = ttk.Label(self, text="username")
        label.pack()
        new_username = ttk.Entry(self)
        new_username.pack()
        label = ttk.Label(self, text="password")
        label.pack()
        new_password = ttk.Entry(self, show="*")
        new_password.pack()
        label = ttk.Label(self, text="retype password")
        label.pack()
        new_passwordverify = ttk.Entry(self, show="*")
        new_passwordverify.pack()
        label = ttk.Label(self, text="house initials")
        label.pack()
        Houseinitial= ttk.Entry(self)
        Houseinitial.pack()
        username = new_username.get()
        password = new_password.get()
        house=Houseinitial.get()
        passwordverify = new_passwordverify.get()

        register = ttk.Button(self, text="Register",
                              command=lambda: self.store(controller, username, password, passwordverify ,house))
        register.pack()
        showpass= ttk.Button(self, text="show password",
                             command=lambda: [new_password.config(show=""), new_passwordverify.config(show="")])
        showpass.pack()
        hidepass = ttk.Button(self, text="hide password",
                              command=lambda: [new_password.config(show="*"), new_passwordverify.config(show="*")])
        hidepass.pack()

        goback = ttk.Button(self, text="Go back", command = lambda: controller.show_frame(menu))
        goback.pack()
    def store(self, controller, username, password, passwordverify, house):
        email = (str(username) + "Marlboroughcollege.org")
        clash=False
        print
        if str(password) == passwordverify:
            cursor.execute("Select username, password from hm_login")
            allhms=cursor.fetchall()
            print(allhms)
            print(allhms[0][0])
            print(username+"hi")
            for i in range(len(allhms)):
                if username == allhms[i][0]:
                    clash=True
                    print("after")
                else:
                    clash=False
            if clash==False:
                print(clash)
                cursor.execute(("INSERT INTO HM(IDHM, house, HmEmail) values(%s, %s, %s) "),(username, house, email))
                cursor.execute(("INSERT INTO HM_login(HM_IDHM, Username, password) values(%s, %s, %s) "),(username, username, password))
                # cnx.commit()
                controller.show_frame(pupil_entry)
        else:
            retry()
        # cursor.execute()
class pupil_entry(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label=ttk.Label(self,text="please enter any new pupils from your house",font=LARGE_FONT)
        label.pack()
        label_id=ttk.Label(self, text="pupil number")
        label_id.pack()
        id_entry=ttk.Entry(self)
        id_entry.pack()
        lable_name=ttk.Label(self, text="pupilname")
        lable_name.pack()
        name_entry=ttk.Entry(self)
        name_entry.pack()
        name = name_entry.get
        id_pupil = id_entry.get
        Enter=ttk.Button(self, text="Enter",
                         command=lambda: self.pupilentry(name,id_pupil))
        Enter.pack()
    def pupilentry(self,name,id_pupil):
        freeslot=True
        global new_username
        global Houseinitial
        cursor.execute("select idpupil, name From pupil")
        in_use=cursor.fetchall()
        for i in range(len(in_use)):
            if in_use[i][0] == id_pupil:
                print("pupil already in system")
                freeslot=False
        if freeslot==True:
            newpupil_insert=("INSERT into pupil(idPupil, name, house, Hm_IDHM) VALUES(%s, %s, %s ,%s)")
            pupil_data=(id_pupil, name, Houseinitial, new_username)
            cursor.execute(newpupil_insert, pupil_data)
class startpage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        global label
        labels = ttk.Label(self, text="Welcome to your Notice Board if you would like to read todays notices please continue", font=LARGE_FONT)
        labels.pack(pady=10, padx=10)
        cursor.execute("SELECT * FROM Notice WHERE Archived = '0'")
        number_of_notice=cursor.fetchall()
        button = tk.Button(self, text=("would you like to read todays notices you have : "+ str(len(number_of_notice)) +" notices"),
                           command=lambda: [controller.show_frame(notice1), start()])
        button.pack()

        button2 = ttk.Button(self, text="would you like to exit",
                            command=quit)
        button2.pack()

class notice1(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        global B
        global pupilids
        label = ttk.Label(self,text=pupilids[0][0],font=LARGE_FONT)
        label.pack(padx=10,pady=10)
        button1 = ttk.Button(self, text="sanctions", command=popup_bonus)
        button1.pack()
        button2 = ttk.Button(self, text="destinctions", command=popup_bonus2)
        button2.pack()
        button3 = ttk.Button(self, text="pupil info", command=popup_showinfo)
        button3.pack()
        button4 = ttk.Button(self, text="Archive", command=archive )
        button4.pack()
        button5 = ttk.Button(self, text="next notice",
                             command=lambda: [controller.show_frame(notice2), forward(), label.config(text=pupilids[B+1][0])])
        button5.pack()
        button6=ttk.Button(self,text="back to original notice",
                            command=lambda: controller.show_frame(startpage))
        button6.pack()
        button7 = ttk.Button(self, text="would you like to exit",
                             command=quit)
        button7.pack()
class notice2(tk.Frame):
    def __init__(self,parent,controller):
        global B
        global pupilids
        print(pupilids[0][0])
        print(B)
        global label
        tk.Frame.__init__(self,parent)
        label = ttk.Label(self,text=pupilids[1][0],font=LARGE_FONT)
        label.pack(padx=10,pady=10)
        button1 = ttk.Button(self, text="sanctions", command=popup_bonus)
        button1.pack()
        # label.config(text=pupilids[B+1][0])
        button2 = ttk.Button(self, text="destinctions", command=popup_bonus2)
        button2.pack()
        button3 = ttk.Button(self, text="pupil info", command=popup_showinfo)
        button3.pack()
        button4 = ttk.Button(self, text="Archive", command=archive )
        button4.pack()
        button5 = ttk.Button(self, text="next notice",
                             command=lambda: [controller.show_frame(notice2), forward(), label.config(text=pupilids[B][0])])
        button5.pack()
        button6 = ttk.Button(self, text="previous notice",
                             command=lambda: [controller.show_frame(notice2), back(), label.config(text=pupilids[B][0])])
        button6.pack()
        button7 = ttk.Button(self, text="back to original notice",
                             command=lambda:[ controller.show_frame(startpage),start(), label.config(text=pupilids[B][0])])
        button7.pack()
        button8 = ttk.Button(self, text="would you like to exit",
                             command=quit)
        button8.pack()
class notice3(tk.Frame):
    def __init__(self,parent,controller):
        global B
        global pupilids
        print(B)
        global label
        tk.Frame.__init__(self,parent)
        label = ttk.Label(self,text=pupilids[2][0],font=LARGE_FONT)
        label.pack(padx=10,pady=10)
        button1 = ttk.Button(self, text="sanctions", command=popup_bonus)
        button1.pack()
        label.config(text=pupilids[B+1][0])
        button2 = ttk.Button(self, text="destinctions", command=popup_bonus2)
        button2.pack()
        button3 = ttk.Button(self, text="pupil info", command=popup_showinfo)
        button3.pack()
        button4 = ttk.Button(self, text="Archive", command=archive )
        button4.pack()
        button5 = ttk.Button(self, text="next notice",
                             command=lambda: [controller.show_frame(notice2), forward(), label.config(text=pupilids[B+1][0])])
        button5.pack()
        button6 = ttk.Button(self, text="previous notice",
                             command=lambda: [controller.show_frame(notice2), back(), label.config(text=pupilids[B-1][0])])
        button6.pack()
        button7 = ttk.Button(self, text="back to original notice",
                             command=lambda:[ controller.show_frame(startpage), label.config(text=pupilids[0][0])])
        button7.pack()
        button8 = ttk.Button(self, text="would you like to exit",
                             command=quit)
        button8.pack()
def qf(quickPrint):
    print(quickPrint)
def main():
    initials=input("please enter you initials")
    notice = Notice(initials)
    notice.pull()
    app = display()
    app.mainloop()
main()

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).