注册用户名和密码

时间:2016-02-13 13:40:23

标签: python python-2.7 authentication tkinter textbox

我的保存和阅读功能无法正常工作。我是否正确保存了文本文件中的数据?我是否正确读取了文本文件中的数据?如果保存和读入文本文件不正确,有人可以纠正我。其他代码我认为也可以。

import Tkinter 
WindowBox = Tkinter.Tk()
WindowBox.geometry("250x200")
WindowBox.title("Welcome to E-UPSR")

getusername1 = Tkinter.StringVar()
getpassword1 = Tkinter.StringVar()
getusername2 = Tkinter.StringVar()
getpassword2 = Tkinter.StringVar()

LabelName = Tkinter.Label (WindowBox, text="Username:")
LabelName.pack()
TxtBoxName = Tkinter.Entry (WindowBox, textvariable= getusername1)
TxtBoxName.pack()

LabelName = Tkinter.Label (WindowBox, text="Password:")
LabelName.pack()
TxtBoxName = Tkinter.Entry (WindowBox, textvariable= getpassword1)
TxtBoxName.pack()

student=[]


def read():
    if len(getusername1.get()) or len(getpassword1.get())==0:
            labelShowName=Tkinter.Label(WindowBox, text="Invalid").pack()
    else:
        addstudent = open ("student.txt", "w")
        addstudent.read("Username:" + getusername1.get())
        addstudent.read("Password: " + getpassword1.get())
        addstudent.close ()
        WindowBox.withdraw()
        MenuBox.deiconify()   
    return

def register():
    WindowBox.withdraw()
    RegBox.deiconify()
    return

RegBox = Tkinter.Tk()
RegBox.geometry("250x200")
RegBox.title("register")

LabelName = Tkinter.Label (RegBox, text="Username:")
LabelName.pack()
TxtBoxName = Tkinter.Entry (RegBox, textvariable= getusername2)
TxtBoxName.pack()
LabelName = Tkinter.Label (RegBox, text="Password:")
LabelName.pack()
TxtBoxName = Tkinter.Entry (RegBox, textvariable= getpassword2)
TxtBoxName.pack()
RegBox.withdraw()

def back():
    RegBox.withdraw()
    WindowBox.deiconify()
    return    


def save():
    while True:
        if len(getusername2.get())== 0 or len(getpassword2.get())== 0:
            labelShowName=Tkinter.Label(RegBox, text="Please key-in").pack()
            break
        else:
            addstudent = open ("student.txt", "w")
            addstudent.write('Username:' + getusername2.get())
            addstudent.write('Password:' + getpassword2.get())
            labelShowName=Tkinter.Label(RegBox, text="Done").pack()
            return
    len(getusername2.get() and getpassword2.get())!= 0
    return

MenuBox = Tkinter.Tk()
MenuBox.geometry("250x200")
MenuBox.title("MainMenu")
MenuBox.withdraw()

BtnName = Tkinter.Button (RegBox, text="Back", command=back).pack()   
BtnName = Tkinter.Button (RegBox, text="Enter", command=save).pack()
BtnName = Tkinter.Button (WindowBox, text="Register", command=register).pack()
BtnName = Tkinter.Button (WindowBox, text="Proceed", command=read).pack()

1 个答案:

答案 0 :(得分:1)

当您编写open ("student.txt", "w")时,每次调用保存方法时,student.txt文件都会被删除。
你需要追加,使用:

open("student.txt", "a")

您使用的是两个Tk窗口,最好使用TopLevel窗口。

read()方法中,要测试密码和用户名:

def read():
   if not getusername1.get() or not getpassword1.get():
       #Your code

save()方法中,您不需要while True

def save():
    if not getusername2.get() or not getpassword2.get():
        #Show an error massage
    else:
        #Save the file

要显示错误消息,您可以使用tkmassagebox.showerror()

import tkMessageBox
.
.
.
tkMessageBox.showerror('Invalid', 'Empty username or password')

您在read()save()方法中写道:

labelShowName=Tkinter.Label(WindowBox, text="Invalid").pack()

labelShowName变量将为None,因为pack()方法不返回任何内容,以保留对您可以使用的Label的引用:

labelShowName=Tkinter.Label(WindowBox, text="Invalid")
labelShowName.pack()

对于所有行都是一样的:

BtnName = Tkinter.Button (RegBox, text="Back", command=back).pack()

改为使用:

BtnName = Tkinter.Button (RegBox, text="Back", command=back)
BtnName.pack()

如果您不需要保留对按钮的引用,请使用:

Tkinter.Button (RegBox, text="Back", command=back).pack()

您使用不同的Tkinter.Entry()小部件使用相同的变量名称,您应该对此感到困惑。

编辑:

import Tkinter 
import tkMessageBox
WindowBox = Tkinter.Tk()
WindowBox.geometry("250x200")
WindowBox.title("Welcome to E-UPSR")


Tkinter.Label (WindowBox, text="Username:").pack()

username1 = Tkinter.Entry (WindowBox)
username1.pack()

Tkinter.Label (WindowBox, text="Password:").pack()

password1 = Tkinter.Entry (WindowBox)
password1.pack()

student=[]


def read():
    if not username1.get() or not password1.get():
        tkMessageBox.showerror('Invalid', 'Empty username or password')
    else:
        addstudent = open ("student.txt", "r")
        lines = addstudent.readlines()
        addstudent.close ()
        i = 0
        while i < len(lines) - 1:
            # username and password are saved in two line, label and value are separated by ':'.
            # to get them we need to reed two line in each iteration and split with ':' to get the value (second result of spliting) then strip to remove end line.
            user = lines[i].split(':')[1].strip()
            password = lines[i+1].split(':')[1].strip()
            # test if the user is registred 
            if user == username1.get() and  password == password1.get():
                WindowBox.withdraw()
                MenuBox.deiconify()
                break
            i += 2  
    return

def register():
    WindowBox.withdraw()
    RegBox.deiconify()
    return

RegBox = Tkinter.Tk()
RegBox.geometry("250x200")
RegBox.title("register")

Tkinter.Label (RegBox, text="Username:").pack()

username2 = Tkinter.Entry (RegBox)
username2.pack()

Tkinter.Label (RegBox, text="Password:").pack()
password2 = Tkinter.Entry (RegBox)
password2.pack()
RegBox.withdraw()

def back():
    RegBox.withdraw()
    WindowBox.deiconify()
    return    


def save():
    if not username2.get() or not password2.get():
        tkMessageBox.showerror('Invalid', 'Empty username or password')
    else:
        addstudent = open ("student.txt", "a")
        addstudent.write('Username:' + username2.get() + '\n')
        addstudent.write('Password:' + password2.get()+'\n')
        tkMessageBox.showinfo("Writing", "Done")
    return

MenuBox = Tkinter.Tk()
MenuBox.geometry("250x200")
MenuBox.title("MainMenu")
MenuBox.withdraw()

Tkinter.Button (RegBox, text="Back", command=back).pack()   
Tkinter.Button (RegBox, text="Enter", command=save).pack()
Tkinter.Button (WindowBox, text="Register", command=register).pack()
Tkinter.Button (WindowBox, text="Proceed", command=read).pack()


WindowBox.mainloop()

我没有使用Tkinter.StringVar()