Demo entry 6353715

NewCode

   

Submitted by anonymous on Apr 02, 2017 at 21:09
Language: Python 3. Code size: 38.0 kB.

#all modules required for the program have been imported.

from tkinter import *
import math
import matplotlib.animation as anim
from matplotlib.pyplot import figure, show
import sqlite3
import webbrowser

###############################################################################
#This Section is The Tkinter section in which all the User Interface is created
#Their are 7 different pages: Login, Signup, Simulator, Revision, progress,
#And 2 seperate ones for the admin which are users and Edit users.
###############################################################################

def login(rootName):
    
    ###################################
    #This page allows the user to login
    ###################################
    
    #############################################
    #This Creates the generic window for the page
    #############################################
    
    rootName="root"                       
    rootName=Tk()                         
    rootName.title("Login")               
    menu(rootName)                        
    rootName["bg"]="steelblue1"         
    rootName.geometry('700x550+350+90')

    #########################################
    #This is where all the labels are Created
    #########################################
    
    loginHeading  = Label(rootName,text="Login",bg="steelblue1",fg="black",font=("Calibri",75,"bold")).place(x=264,y=10)    
    usernameLabel = Label(rootName,text="Username",bg="steelblue1",font=("Calibri",16,"bold")).place(x=235,y=252)           
    passwordLabel = Label(rootName,text="Password",bg="steelblue1",font=("Calibri",16,"bold")).place(x=242,y=285)
    newUserLabel = Label(rootName,text="New user? ",bg="steelblue1",font=("Calibri",16,"bold")).place(x=230,y=430)
    

    ##########################################
    #This is where all the Entries are Created
    ##########################################
    
    usernameEntry = Entry(rootName,bg="white",width="25",relief=SUNKEN)
    usernameEntry.place(x=335,y=260)
    passwordEntry = Entry(rootName,bg="white",width="25",show="*",relief=SUNKEN)
    passwordEntry.place(x=335,y=292)

    ##########################################
    #This is where all the Buttons are Created
    ##########################################
    
    loginInButton=Button(rootName,command=lambda: loginCheck(usernameEntry.get(),passwordEntry.get(),rootName),text="Sign in",bg="navy",fg="white",activebackground="light blue",activeforeground="black",relief=RAISED,width=10,font=("bold")).place(x=335, y=330)
    signUpButton=Button(rootName,text="Sign up", command=lambda: signUp(rootName),bg="navy",fg="white",activebackground="light blue",activeforeground="Black",relief=RAISED,width=10,font=("bold")).place(x=335, y=430)  
    exitButton=Button(rootName,cursor="hand2",text="Exit",fg="white",bg="navy",command=lambda: exit(rootName),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=500)

def signUp(rootName):
    
    rootName.destroy()
    rootName=Tk()
    rootName.title("Sign up")
    menu(rootName)
    rootName["bg"]="steelblue1"
    rootName.geometry('700x550+350+90')

    ###########################################################
    #This is the Sign up page which allows new users to sign up
    ###########################################################

    signUpHeading=Label(text="Sign Up",bg="steelblue1",fg="black",font=("Calibri",60,"bold")).place(x=265,y=25)
    usernameLabel = Label(rootName,text="Username",bg="steelblue1",font=("Calibri",16,"bold")).place(x=235,y=252)
    passwordLabel = Label(rootName,text="Password",bg="steelblue1",font=("Calibri",16,"bold")).place(x=242,y=285)
    retypePassowrdLabel = Label(rootName,text="Retype Password",bg="steelblue1",font=("Calibri",16,"bold")).place(x=175,y=320)

    ##########################################

    usernameEntry = Entry(rootName,bg="White",width="25",relief=SUNKEN)
    usernameEntry.place(x=335,y=260)
    passwordEntry = Entry(rootName,bg="White",width="25",show="*",relief=SUNKEN)
    passwordEntry.place(x=335,y=292)
    retypePassEntry = Entry(rootName,bg="White",width="25",show="*",relief=SUNKEN)
    retypePassEntry.place(x=335,y=328)

    ##########################################
    
    signUpbutton=Button(rootName,text="Sign up",bg="navy",command=lambda: data_entry(usernameEntry.get(),passwordEntry.get(),retypePassEntry.get(),rootName),fg="white",activebackground="light blue",activeforeground="Black",relief=RAISED,width=10,font=("bold")).place(x=335, y=360)
    backButton=Button(rootName,text="Back",fg="white",bg="navy",command=lambda: backFromSign(rootName),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=500)

def simulator(rootName,username,admin):

    rootName.destroy()
    rootName=Tk()
    rootName.title("Simulator")
    menu(rootName)
    rootName["bg"]="steelblue1"
    rootName.geometry('700x550+350+90')
    
    ###########################################################################
    #This is the Simulator page it allows the user to input values and siumalte
    ###########################################################################

    signedInNameLabel = Label(rootName,text='Signed in as : {}'.format(username),bg="steelblue1",fg="black",font=("Calibri",12,"bold")).place(x=7,y=5)
    simulatorLabel=Label(text="Simulator",bg="steelblue1",fg="black",font=("Calibri",60,"bold")).place(x=210,y=25)
    
    initalVelocityLabel = Label(rootName,text="Inital Velocity=",bg="steelblue1",font=("Calibri",25,"bold")).place(x=199,y=180)
    displacementLabel = Label(rootName,text="Displacement=",bg="steelblue1",font=("Calibri",25,"bold")).place(x=200,y=240)
    angleLabel = Label(rootName,text="Angle=",bg="steelblue1",font=("Calibri",25,"bold")).place(x=309,y=300)
    gravityLabel = Label(rootName,text="Gravity (9.81)=",bg="steelblue1",font=("Calibri",25,"bold")).place(x=200,y=360)

    ##########################################
    
    initalVelocityEntry = (Entry(rootName,bg="White",width="20",relief=SUNKEN))
    initalVelocityEntry.place(x=452,y=193)
    displacementEntry = Entry(rootName,bg="White",width="20",relief=SUNKEN)
    displacementEntry.place(x=452,y=253)
    angleEntry = Entry(rootName,bg="White",width="20",relief=SUNKEN)
    angleEntry.place(x=452,y=313)
    gravityEntry = Entry(rootName,bg="White",width="20",relief=SUNKEN)
    gravityEntry.place(x=452,y=373)

    #########################################################################
    # admin stores the value which decides which widgets to show depending on
    # who logs in I.e student or Teacher/Admin
    #########################################################################

    if admin==2:  
        usersButton=Button(rootName,text="Users",fg="white",bg="navy",command=lambda:users(rootName,username,admin),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=180)
        
    if admin==1 :
        progressButton=Button(rootName,text="Progress",command=lambda:progress(rootName,username,admin),fg="white",bg="navy",width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=180)

    questionsButton=Button(rootName,text="Questions",command=lambda:Questions(rootName,username,admin),fg="white",bg="navy",width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=260)     
    simulateButton=Button(rootName,text="Simulate",fg="white",bg="navy",command=lambda: validateInputs(rootName,username,admin,initalVelocityEntry.get(),displacementEntry.get(),angleEntry.get(),gravityEntry.get()),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=464,y=425)
    exitButton=Button(rootName,text="Exit",fg="white",bg="navy",command=lambda: exit(rootName),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=500)
    revisionButton=Button(rootName,text="Revision",command=lambda:revision(rootName,username,admin),fg="white",bg="navy",width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=220)
    LogOutButton=Button(rootName,text="Log out",fg="white",bg="navy",command=lambda:backFromSign(rootName),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=35)
            
        
def revision(rootName,username,admin):
    
    rootName.destroy()
    rootName=Tk()
    rootName.title("Revision")
    menu(rootName)
    rootName["bg"]="steelblue1"
    rootName.geometry('700x550+350+90')

    #############################################################################
    #This section is where the links to revision websites and Videos are created.
    #############################################################################

    revisionHeading=Label(text="Revision",bg="steelblue1",fg="black",font=("Calibri",60,"bold")).place(x=210,y=25)
    
    subHeading = Label(rootName,text="Usefull websites for revision :",bg="steelblue1",font=("Calibri",20,"bold"),fg="Black").place(x=180,y=150)
    link1=Label(rootName,text="Scool projectile motion",bg="steelblue1",font=("Calibri",15,"bold"),fg="Blue",cursor="hand2")
    link1.place(x=190,y=200)
    link1.bind("<Button-1>", lambda event: openPage(1))

    link2=Label(rootName,text="Revision maths Mechanics",bg="steelblue1",font=("Calibri",15,"bold"),fg="Blue",cursor="hand2")
    link2.place(x=190,y=240)
    link2.bind("<Button-1>", lambda event: openPage(2))

    link3=Label(rootName,text="BBC Bitesize projectile motion",bg="steelblue1",font=("Calibri",15,"bold"),fg="Blue",cursor="hand2")
    link3.place(x=190,y=280)
    link3.bind("<Button-1>", lambda event: openPage(3))

    subHeading2= Label(rootName,text="Usefull videos for revision :",bg="steelblue1",font=("Calibri",20,"bold"),fg="Black").place(x=180,y=320)

    link4=Label(rootName,text="Projectiles and equations of motion",bg="steelblue1",font=("Calibri",15,"bold"),fg="Blue",cursor="hand2")
    link4.place(x=190,y=370)
    link4.bind("<Button-1>", lambda event: openPage(4))

    link5=Label(rootName,text="Projectile Motion",bg="steelblue1",font=("Calibri",15,"bold"),fg="Blue",cursor="hand2")
    link5.place(x=190,y=410)
    link5.bind("<Button-1>", lambda event: openPage(5))

    link6=Label(rootName,text="Khan Academy Projectile motion",bg="steelblue1",font=("Calibri",15,"bold"),fg="Blue",cursor="hand2")
    link6.place(x=190,y=450)
    link6.bind("<Button-1>", lambda event: openPage(6))

    if admin==2:
        simulatorButton=Button(rootName,text="Simulator",fg="white",bg="navy",command=lambda: backFromProgress(rootName,username,admin),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=140)
        
    else:    
        simulatorButton=Button(rootName,text="Simulator",fg="white",bg="navy",command=lambda: backFromProgress(rootName,username,admin),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=220)
        progressButton=Button(rootName,text="Progress",command=lambda:progress(rootName,username,admin),fg="white",bg="navy",width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=180)
    
    backButton=Button(rootName,text="Back",fg="white",bg="navy",command=lambda: backFromRevision(rootName,username,admin),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=500)

def progress(rootName,username,admin):
    
    rootName.destroy()
    rootName=Tk()
    rootName.title("Progress")
    menu(rootName)
    rootName["bg"]="steelblue1"
    rootName.geometry('700x550+350+90')

    #######################################################
    #This section shows the progress and target to the user
    #######################################################

    heading=Label(text="Progress",bg="steelblue1",fg="black",font=("Calibri",60,"bold")).place(x=210,y=25)
    signedInNameLabel = Label(rootName,text='Signed in as : {}'.format(username),bg="steelblue1",fg="black",font=("Calibri",12,"bold")).place(x=7,y=5)
    
    
    if admin==1:
        progress= Label(rootName,text="Current progress:",bg="steelblue1",font=("Calibri",20,"bold"),fg="Black").place(x=150,y=200)
        progressBox= Label(rootName,text="",bg="white",font=("Calibri",20,"bold"),fg="Black").place(x=380,y=200,width=200,height=40)
        target= Label(rootName,text="Target:",bg="steelblue1",font=("Calibri",20,"bold"),fg="Black").place(x=265,y=300)
        targetBox= Label(rootName,text="",bg="white",font=("Calibri",20,"bold"),fg="Black").place(x=380,y=300,width=200,height=40)
        progress2=20
        progressShow(rootName,username,admin)

    backButton=Button(rootName,text="Back",fg="white",bg="navy",command=lambda: backFromProgress(rootName,username,admin),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=500)
        
def users(rootName,username,admin):
    
    rootName.destroy()
    rootName=Tk()
    rootName.title("Users")
    menu(rootName)
    rootName["bg"]="steelblue1"
    rootName.geometry('700x550+350+90')

    ####################################################################
    #This section allows the Teacher/admin to edit the users who sign up
    #The info of each student is located in the listbox
    #This section can only be accessed by the Teacher/admin
    ####################################################################

    userLabel=Label(text="Users",bg="steelblue1",fg="black",font=("Calibri",60,"bold")).place(x=260,y=25)
    
    if admin==2:
        scrollbar = Scrollbar(rootName, orient=VERTICAL)        
        listbox = Listbox(rootName,yscrollcommand=scrollbar.set,selectmode=SINGLE,font=("Courier New",10,"bold"))
        listbox.place(x=70,y=150,width=550)
        scrollbar.config(command=listbox.yview)
        scrollbar.pack(side=RIGHT, fill=Y)

        listbox.insert(END,"user ID : target : progress : Username : password")
        listbox.insert(END,"")

        
        progress1=retrieveProgress()
        
        for item in progress1:
            newlist=[item[2],":",item[1],":",item[0],":",item[3],":",item[4]]
            listbox.insert(END, newlist)
    
    ##########################################
            
    deleteButton=Button(rootName,text="Delete",fg="white",bg="navy",command=lambda: delete(listbox,newlist),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=500,y=410)
    editButton=Button(rootName,text="Edit",fg="white",bg="navy",command=lambda:edit(listbox,newlist),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=500,y=360)
    backButton=Button(rootName,text="Back",fg="white",bg="navy",command=lambda: backFromUsers(rootName,username,admin),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=500)
    refreshButton=Button(rootName,text="Refresh",fg="white",bg="navy",command=lambda: users(rootName,username,admin),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=500,y=460)

  
def edit(listbox,newlist):
    
    rootName=Tk()
    rootName.title("Edit")
    menu(rootName)
    rootName["bg"]="steelblue1"
    rootName.geometry('350x350+475+490')

    ##################################################################
    #This section selects the user the Teacher/admin have selected
    #and allows for the credentials of that selected user to be edited
    ##################################################################

    selection = listbox.curselection()
    selection1=listbox.get(listbox.curselection())
    selection2=(selection1[1])
    selection3=(selection1[2])
    if selection3==",":
        selection4=selection2
    else:
        selection4=int(selection2+selection3)

    selection4=selection1[0]


    ##########################################
    
    username= Label(rootName,text="Username",bg="steelblue1",font=("Calibri",15,"bold"),fg="Black").place(x=50,y=29,height=40)
    entry_1 = Entry(rootName,bg="White",width="25",relief=SUNKEN)
    entry_1.place(x=150,y=40)
    
    password= Label(rootName,text="Password",bg="steelblue1",font=("Calibri",15,"bold"),fg="Black").place(x=50,y=90,height=40)
    entry_2 = Entry(rootName,bg="White",width="25",relief=SUNKEN)
    entry_2.place(x=150,y=100)
    
    target= Label(rootName,text="Target",bg="steelblue1",font=("Calibri",15,"bold"),fg="Black").place(x=75,y=150,height=40)
    entry_3 = Entry(rootName,bg="White",width="25",relief=SUNKEN)
    entry_3.place(x=150,y=160)
   
    progress= Label(rootName,text="Progress",bg="steelblue1",font=("Calibri",15,"bold"),fg="Black").place(x=57,y=210,height=40)
    entry_4 = Entry(rootName,bg="White",width="25",relief=SUNKEN)
    entry_4.place(x=150,y=220)
    
    Update=Button(rootName,command=lambda:updated(entry_1.get(),entry_2.get(),entry_3.get(),entry_4.get(),selection4),text="Update",fg="white",bg="navy",width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=175,y=290)

def Questions(rootName,username,admin):
    
    rootName.destroy()
    rootName=Tk()
    rootName.title("Users")
    menu(rootName)
    rootName["bg"]="steelblue1"
    rootName.geometry('700x550+350+90')

    #######################################################################
    #This section allows the admin/Teacher to add questions to the database
    #which then allows the students to access them.
    #######################################################################

    questionLabel=Label(text="Questions",bg="steelblue1",fg="black",font=("Calibri",60,"bold")).place(x=200,y=25)
    if admin == 1:
        scrollbar = Scrollbar(rootName, orient=VERTICAL)        
        listbox = Listbox(rootName,yscrollcommand=scrollbar.set,selectmode=SINGLE,font=("Courier New",10,"bold"))
        listbox.place(x=130,y=150,width=530)
        scrollbar.config(command=listbox.yview)
        scrollbar.pack(side=RIGHT, fill=Y)

        listbox.insert(END,"Question")
        listbox.insert(END,"")

        Questions=retrieveQuestions()
        for item in Questions:
            newlist=[item[0]]
            listbox.insert(END, newlist)

        OpenButton=Button(rootName,text="Open",fg="white",bg="navy",command=lambda: openLink(listbox),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=500,y=379)

    if admin==2:

        scrollbar = Scrollbar(rootName, orient=VERTICAL)        
        listbox = Listbox(rootName,yscrollcommand=scrollbar.set,selectmode=SINGLE,font=("Courier New",10,"bold"))
        listbox.place(x=130,y=150,width=530)
        scrollbar.config(command=listbox.yview)
        scrollbar.pack(side=RIGHT, fill=Y)

        QuestionLink= Label(rootName,text="Add Question",bg="steelblue1",font=("Calibri",15,"bold"),fg="Black").place(x=130,y=335,height=40)
        entry_1 = Entry(rootName,bg="White",width="50",relief=SUNKEN,font=("Calibri",10,"bold"))
        entry_1.place(x=130,y=380,height="30")

        listbox.insert(END,"Question")
        listbox.insert(END,"")

        Questions=retrieveQuestions()
        for item in Questions:
            newlist=[item[0]]
            listbox.insert(END, newlist)


        AddButton=Button(rootName,text="Add",fg="white",bg="navy",command=lambda: Add(listbox,entry_1.get(),rootName,username,admin),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=500,y=379)
        DeleteQButton=Button(rootName,text="Delete",fg="white",bg="navy",command=lambda: DeleteQ(listbox,rootName,username,admin),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=500,y=419)
     
    revisionButton=Button(rootName,text="Revision",command=lambda:revision(rootName,username,admin),fg="white",bg="navy",width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=220)
    backButton=Button(rootName,text="Back",fg="white",bg="navy",command=lambda: backFromQuestions(rootName,username,admin),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=500)
    simulatorButton=Button(rootName,text="Simulator",fg="white",bg="navy",command=lambda: backFromProgress(rootName,username,admin),width="10",activebackground="light blue",activeforeground="Black",relief=RAISED,font=("bold")).place(x=10,y=180)
        

##########################################################################################################################################################################################################################################################################

#############################################################################
#The following section are all the functions required by the different pages
#e.g. The progress page requires a check progress function
#############################################################################

#############################################################################
#The Menu is a drop down box which leads to a help section which explains
#how the program works. 
#############################################################################


def menu(rootName):
    menu= Menu(rootName)
    rootName.config(menu=menu)
    subMenu= Menu(menu)
    menu.add_cascade(label="Help",menu=subMenu)
    subMenu.add_command(label="About this program",command=showHelp)
    

def showHelp():
    
    rootName=Tk()
    rootName.title("Help")
    rootName["bg"]="steelblue1"
    rootName.geometry('350x280')

    helpLabel= Label(rootName,text="""This program simulates the motion
    of a projectile, To use this program
    you must first login, if you dont have
    a login then sign up. once you have logged
    in you can use the simulator as well as
    checking your current progress and
    target percentages which will be set by
    your teacher. There is also a revision
    section which has links to usefull websites
    and videos as well as past paper questions
    on projectile motion."""
    ,bg="steelblue1",font=("Calibri",12,"bold"),fg="Black").place(x=10,y=29,)
    

################################################################
#These functions allow for the user to navigate around the pages
################################################################
    
def backFromSign(rootName):
    rootName.destroy()
    login(rootName)

def backFromProgress(rootName,username,admin):
    simulator(rootName,username,admin)

def backFromRevision(rootName,username,admin):
    simulator(rootName,username,admin)

def backFromUsers(rootName,username,admin):
    simulator(rootName,username,admin)

def backFromQuestions(rootName,username,admin):
    simulator(rootName,username,admin)
    
def exit(rootName):
    rootName.destroy()

##########################################################
#These functions are for error handling and login checking
##########################################################

def loginCheck(username,password,rootName):
    Credentials=retrieveUsers()
    loginTrue=False

    for sublist in Credentials:
        if sublist[0]==username:
            if sublist[1]==password:
                admin=1
                simulator(rootName,username,admin)
                loginTrue=True
                break

        elif username=="Teacherlogin9":   
            if password=="adminpassword9": 
                admin=2
                simulator(rootName,username,admin)
                loginTrue=True
                break
   
    if loginTrue==False:
        value=1
        button_pressed(rootName,value)
    
def button_pressed(rootName,value):
    
    if value==1:
        label=Label(rootName,text="Incorrect username or password",bg="steelblue1",fg="red",font=("Calibri",12,"bold"))
        label.place(x=265,y=220)
        rootName.after(2000,label.destroy)
        
    elif value==2:
        label=Label(rootName,text="Passwords do not match",bg="steelblue1",fg="red",font=("Calibri",12,"bold"))
        label.place(x=300,y=200)
        rootName.after(2000,label.destroy)
        
    elif value==3:
        label=Label(rootName,text="Username/password cannot be left blank",bg="steelblue1",fg="red",font=("Calibri",12,"bold"))
        label.place(x=260,y=200)
        rootName.after(2000,label.destroy)

    elif value==4:
        label=Label(rootName,text="Username has allready been taken",bg="steelblue1",fg="red",font=("Calibri",12,"bold"))
        label.place(x=265,y=200)
        rootName.after(2000,label.destroy)

    elif value==5:
        backFromSign(rootName)

    elif value==6:
        label=Label(rootName,text="Username/password can only consist of Alphabet and numbers and symbols",bg="steelblue1",fg="red",font=("Calibri",12,"bold"))
        label.place(x=100,y=200)
        rootName.after(2000,label.destroy)

    elif value==7:
        label=Label(rootName,text="Please enter a number/Flaot",bg="steelblue1",fg="red",font=("Calibri",12,"bold"))
        label.place(x=400,y=150)
        rootName.after(2000,label.destroy)


def validateInputs(rootName,username,admin,entry1,entry2,entry3,entry4):
    try:
        entry1=int(entry1)
        entry2=int(entry2)
        entry3=int(entry3)
        entry4=int(entry4)
        if entry1== int(entry1) and entry2== int(entry2) and entry3== int(entry3) and entry4== int(entry4) :
            simulate(entry1,entry2,entry3,entry4)
            
    except ValueError:
            value=7
            button_pressed(rootName,value)
    


#########################################################
#Checks to see if there are letter inputs in the username
#########################################################
        
def checkForLetters(userName):
    store=re.search('[a-zA-Z]', userName)
    if store:
        return True
    else:
        return False
    
###################################
#Gives each user a unique id number
###################################

def userId():
    c.execute("SELECT * FROM Credentials ORDER BY userId DESC LIMIT 1")
    store = c.fetchall()
    if store==[]:
        value=1
    else:
        value=store[0][0]      
    return value

###########################################################
#The following function validates the username and password
#and if everything checks out then it stores the username
#and password in the database.
###########################################################

def data_entry(entry1,entry2,entry3,rootName):
    Credentials=retrieveUsers()
    userName=entry1
    password=entry2
    validation=checkForLetters(userName)
    spaceName=userName.strip()
    spacePass=password.strip()
    if userName==spaceName and password==spacePass:
        if validation==True: 
            valid=True
            for sublist in Credentials:
                if userName==sublist[0]:
                    valid=False
                    value=4
                    button_pressed(rootName,value)
                    
            if valid==True:    
                if userName != "":
                    if password !="":
                        if entry2==entry3:
                            password=entry2
                            progress=0
                            target=0
                            c.execute("INSERT INTO Credentials (userName,password) VALUES (?,?)",
                                 (userName,password))
                            user=userId()
                            c.execute("INSERT INTO Progress (progress,target,userId) VALUES (?,?,?)",
                                 (progress,target,user))
                            value=5
                            button_pressed(rootName,value)
                        else:
                            value=2
                            button_pressed(rootName,value)
        else:
            value=3
            button_pressed(rootName,value)
    else:
        value=6
        button_pressed(rootName,value)

        
    conn.commit()
                                      
                
def retrieveUsers():
    c.execute("SELECT userName, password FROM Credentials")
    users = c.fetchall()
    return users

#############################################################
#The following function stores the weblinks and question file
#located on the computer
#############################################################

def openPage(value):
    if value==1:
        webbrowser.open_new(r"http://www.s-cool.co.uk/a-level/physics/equations-of-motion/revise-it/projectile-motion-and-what-if-velocity-and-acceleratio")
    elif value==2:
        webbrowser.open_new(r"https://revisionmaths.com/advanced-level-maths-revision/mechanics/projectiles")
    elif value==3:
        webbrowser.open_new(r"http://www.bbc.co.uk/education/guides/zrsdmp3/revision")
    elif value==4:
        webbrowser.open_new(r"https://www.youtube.com/watch?v=tJJpKVSqMcw")
    elif value==5:
        webbrowser.open_new(r"https://www.youtube.com/watch?v=9thdmEdCcKw")
    elif value==6:
        webbrowser.open_new(r"https://www.youtube.com/watch?v=15zliAL4llE")
    elif value==7:
        webbrowser.open_new(r'F:\Users\Samir\Desktop\PracticeQs.pdf') #Set This to the location of the file.


def openLink(listbox):
    selection = listbox.curselection()
    selection1=listbox.get(listbox.curselection())
    selection2=(selection1[0])
    webbrowser.open_new(r"{}".format(selection2))
    
    
##############################################################
#The following function deletes the user selected by the admin
##############################################################

def delete(listbox,newlist):
    
    # Delete from Listbox
    selection = listbox.curselection()
    selection1=listbox.get(listbox.curselection())
    listbox.delete(selection[0])
    selection2=(selection1[1])
    selection3=(selection1[2])
    if selection3==",":
        selection4=selection2
    else:
        selection4=int(selection2+selection3)

    selection4=selection1[0]
    
    c.execute("DELETE FROM Credentials WHERE userID=?", (selection4,))
    c.execute("DELETE FROM Progress WHERE userID=?", (selection4,))
    conn.commit()
    
#######################################################
#This Adds the link to the questions into the database.
#######################################################

def Add(listbox,entry,rootName,username,admin):

    Completed=0
    c.execute("INSERT INTO Questions (Questions,Completed) VALUES (?,?)",(entry,Completed))
    conn.commit()
    Questions(rootName,username,admin)

##########################################################
#This deletes the link to the questions into the database.
##########################################################

def DeleteQ(listbox,rootName,username,admin):
    
    selection = listbox.curselection()
    selection1=listbox.get(listbox.curselection())
    selection2=(selection1[0])
    
    c.execute("DELETE FROM Questions WHERE Questions=?", (selection2,))
    conn.commit()

    Questions(rootName,username,admin)

#################################################################
#This retrieves the link from the questions table in the database.
#################################################################

def retrieveQuestions():
    c.execute("SELECT * FROM Questions")
    users = c.fetchall()
    return users
    

#####################################################################
#The following function Updates the users credentials in the database
#####################################################################


def updated(newName,newPass,newTar,newProg,selection4):

    user1=retrieveUsers()
    
    c.execute("UPDATE Credentials SET userName=? ,password=? where userID=?", (newName,newPass,selection4))
    c.execute("UPDATE progress SET progress=?, target=? where userID=?", (newProg,newTar,selection4))
    conn.commit()
    

#######################################################################                                    
#The following function retrieves the progress of the user from the
#database and the function below that, displays the progress and target
#######################################################################
                                      

def progressShow(rootName,username,admin):
    progress1=retrieveProgress()
    Credentials=retrieveUsers()
    print(progress1)

    for sublist in progress1:
        if sublist[3]==username:
            progress2=sublist[0]
            target=sublist[1]
            progressLabel=Label(rootName,text=(progress2,"%"),bg="white",fg="red",font=("Calibri",20,"bold")).place(x=450,y=200)
            targetLabel=Label(rootName,text=(target,"%"),bg="white",fg="red",font=("Calibri",20,"bold")).place(x=450,y=300)
            progressCheck=True
            break

    if progress2 > target:
        label=Label(rootName,text=("Well done, you are currently exceeding your target!"),bg="steelblue1",fg="green",font=("Calibri",15,"bold")).place(x=160,y=400)

    elif progress2 == target:
        label=Label(rootName,text=("Well done, you are currently on target!"),bg="steelblue1",fg="green",font=("Calibri",15,"bold")).place(x=220,y=400)
    
    elif progress2 < target:
        label=Label(rootName,text=("You are currently below your target, work harder!"),bg="steelblue1",fg="Red",font=("Calibri",15,"bold")).place(x=160,y=400)
       
def retrieveProgress():
    c.execute("SELECT * FROM Progress NATURAL JOIN Credentials")
    users = c.fetchall()
    return users
        
##########################################################
#The following function creates the tables in the database
##########################################################

def create_table():
    c.execute("""CREATE TABLE IF NOT EXISTS Credentials
    (UserId INTEGER PRIMARY KEY AUTOINCREMENT,
    userName TEXT,
    password TEXT)
    """)

    c.execute("""CREATE TABLE IF NOT EXISTS Progress
    (progress INTEGER,
    target INTEGER,
    userId INTEGER,
    FOREIGN KEY(userId) REFERENCES Credentials(userId))
    """)

    c.execute("""CREATE TABLE IF NOT EXISTS Questions
    (Questions TEXT,
    Completed INTEGER)
    """)

##########################################################################################################################################################################################################################################################################

############################################################################
#The final function is the main simulator which uses the matplotlib module
#to display the simulation of a projectile, it has self containing functions
#this is so the flow of data is easily managed.
############################################################################


def simulate(entry1,entry2,entry3,entry4):
        
    def projectileMotion():
        x = 0
        y = 0
        xc = []
        yc = []
        t=0
        while t < time:
            x = initialV * math.cos(angleNew) * t
            y = initialV * math.sin(angleNew) * t + (g* t**2) / 2 + height
            if y < 0:
                break
            t += dt
            xc.append(x)
            yc.append(y)
            yield x, y

    def frame(sim):
        x, y = sim[0], sim[1]
        line.set_data(x, y)
        return line,

    def init():
        line.set_data([], [])
        return line,
    
    angle=float(entry3)
    g=float(entry4)
    g=(-g)

    initialV=float(entry1)

    if entry2=="":
        height=0
    else:
        height=float(entry2)
        
    angleNew= angle * math.pi /180
    dt=0.1
    time = -initialV**2 * math.sin(2 * angleNew) / g
    
    figure1 = figure()
    ax = figure1.add_subplot(111)
    ax.set_xlim([-5,1500])
    ax.set_ylim([0,300])

    line, = ax.plot([], [], 'ro', ms=10 ) 
    ani = anim.FuncAnimation(fig=figure1, func=frame, frames=projectileMotion, blit=False, interval=20, init_func=init, repeat=False)
    show()
    
    #ax.plot() returns a tuple with one element. with the addtion of the comma to the assignment target list,
    #Python will unpack the return value and assign it to each variable named to the left in turn which in this case is "line". 
    
                                            

##########################################################################################################################################################################################################################################################################

conn = sqlite3.connect("Login.db")
c = conn.cursor()

create_table()   
root=""
login(root)












    

This snippet took 0.06 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).