2017-12-27 1025 views
-2

我想知道如何不用保存到文件,而是保存到大量用戶的數據庫/文件。 > 50.如何使用內置的Python 3.5模塊爲登錄腳本創建數據庫

保存到文件的部分是下面:

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() 

這是我的主要代碼:

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() 
+0

THX編輯稍微凌亂,因爲未使用本網站的格式 – aPythonUser

回答