注册用户名和密码

2024-06-28 20:52:18 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: textgetlentkinterlabelpacklabelnamewithdraw
1条回答
网友
1楼 · 发布于 2024-06-28 20:52:18

当您编写open ("student.txt", "w")时,student.txt文件将在每次调用save方法时生成。
您需要附加,请使用:

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

您正在使用两个Tk窗口,最好使用TopLevel窗口。在

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

^{pr2}$

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

相关问题 更多 >