How to make a database using inbuilt Python 3.5 modules for login script

python database login

24 观看

1回复

3 作者的声誉

I am wondering how to instead of saving to file, save to a database/file with large amount of users e.g. > 50.

The save to file part is below:

def FSSignup():#This defines FSSignup

    with open(creds, 'w') as f:
        f.write(nameE.get())
        f.write('\n')
        f.write(pwordE.get())
        f.close()

    roots.destroy()
    login()

This is my main code:

from tkinter import *
import os
creds = "tempfile.temp"

def signup():
    global pwordE
    global nameE
    global roots

    roots = Tk()
    roots.title("Signup")
    instructions = Label(roots, text="Please enter new credentials:")
    instructions.grid(row=0, column=0, sticky=E)

    nameL = Label(roots, text="New Username")
    pwordL = Label(roots, text = "New Password")
    nameL.grid(row=1, column=0, sticky=W)
    pwordL.grid(row=2, column=0, sticky=W)

    nameE = Entry(roots)
    pwordE= Entry(roots, show='"')
    nameE.grid(row=1, column=1)
    pwordE.grid(row=2, column=1)

    signupB = Button(roots, text="Signup", command=FSSignup)
    signupB.grid(columnspan=2, sticky=W)

    roots.mainloop()#

def FSSignup():

    with open(creds, 'w') as f:
        f.write(nameE.get())
        f.write('\n')
        f.write(pwordE.get())
        f.close()

    roots.destroy()
    login()

def login():
    global nameEL
    global pwordEL
    global rootA

    rootA = Tk()
    rootA.title("Login")

    instruction = Label(rootA, text="Enter your credentials")
    instruction.grid(sticky=E)

    nameL = Label(rootA, text="Username")
    pwordL = Label(rootA, text="Password")
    nameL.grid(row=1, sticky=W)
    pwordL.grid(row=2, sticky=W)

    nameEL = Entry(rootA)
    pwordEL = Entry(rootA, show='"')
    nameEL.grid(row=1, column=1)
    pwordEL.grid(row=2, column=1)

    loginB = Button(rootA, text="Login", command=CheckLogin)
    loginB.grid(columnspan=2, sticky=W)

    rmuser = Button(rootA, text="Delete User", fg="red", command=DelUser)
    rmuser.grid(columnspan=2, sticky=W)

    rootA.mainloop()
def CheckLogin():
    with open(creds) as f:
        data = f.readlines()
        username = data[0].rstrip()
        pword = data[1].rstrip()

    if nameEL.get() == username and pwordEL.get() == pword:
        r =Tk()
        r.title("Successful Login")
        r.geometry("150x50")
        rL = Label(r, text="\n[+]Logged In")
        rL.pack()
        r.mainloop()
    else:
        r =Tk()
        r.title("Invalid Login")
        r.geometry("150x50")
        rL = Label(r, text="\n[+]Invalid Login")
        rL.pack()
        r.mainloop()

def DelUser():
    os.remove(creds)
    rootA.destroy
    signup()

if os.path.isfile(creds):
    login()
else:
    signup()
作者: aPythonUser 的来源 发布者: 2017 年 12 月 27 日

回应 1


0

1608 作者的声誉

SQLite is part of the standard library -- https://docs.python.org/3.5/library/sqlite3.html

作者: Mario 发布者: 2017 年 12 月 27 日
32x32