python - 注册用户名和密码

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

最佳答案

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

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

您正在使用两个 Tk windows,最好使用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() .

关于python - 注册用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35380641/

相关文章:

Python re regex 子字母不包含在引号中,并且如果它们匹配特定单词(包括正则表达式组/匹配)则不包含在引号中

javascript - 无法使用带有 ssh2 模块的 nodejs 进行身份验证,即使手动 ssh 有效

php - 如何在 android 中处理远程网站的身份验证?

python - paramiko 中用于身份验证类型的 get_allowed_auths()

python - multiprocessing.Process() 或 multiprocessing.Pool() 会更均匀地分布在核心之间吗?

python - 为什么 Python 2 中的类型顺序是固定的,而 Python 3 中的类型错误是不可排序的?

python - Pandas :返回 NaN 行

python - 预期单例 : hr. employee(1, 2)

python - Pandas :水平扩展/分解数据框

python-2.7 - 为什么我们得到一个 'Client' 对象没有 Google BigQuery 的属性 'get_dataset'?