认证程序

2024-10-03 19:26:16 发布

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

我有两个界面:登录和注册。在

我在将用户名和密码数据保存到文本文件时出错。在

我在读回数据以进行身份验证时遇到了一个问题。在

这是我的代码:

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

getusername = Tkinter.StringVar()
getpassword = Tkinter.StringVar()

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

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

tudent=[]

def read():
    addstudent = open ("student.txt", "w")
    addstudent.write("User ID: " + entry_box1.get())
    addstudent.write("\nUser Password: " + entry_box2.get())
    addstudent.close ()

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

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

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

LabelName = Tkinter.Label (RegBox, text="Enter Username:")
LabelName.pack()
TxtBoxName = Tkinter.Entry (RegBox, textvariable= getusername)
TxtBoxName.pack()

LabelName = Tkinter.Label (RegBox, text="Enter Password:")
LabelName.pack()
TxtBoxName = Tkinter.Entry (RegBox, textvariable= getpassword)
TxtBoxName.pack()
RegBox.withdraw()

def save():
    getusername=entry_box1.get()
    getpassword=entry_box2.get()
    addstudent = open ("student.txt", "w")
    addstudent.write("Username:" + entry_box1.get())
    addstudent.write("Password: " + entry_box2.get())
    addstudent.close ()

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

WindowBox.mainloop()

Tags: textgettkinterlabelpackentrytextvariablelabelname
3条回答

第41和46行:

TxtBoxName = Tkinter.Entry (RegBox, textvariable= getusername)

TxtBoxName = Tkinter.Entry (RegBox, textvariable= getpassword)

它们需要被指定为变量,在代码中,它们共享相同的变量名(或者说,每次声明名为“TxtBoxName”的新元素时,“TxtBoxName”值都会被覆盖。它们仍然出现的原因是您已经将它们的值打包到小部件中)。在

当您在第56行和第57行调用Entry.get()时:

^{pr2}$

对不存在的变量名调用get方法。TxtBoxName.get()但是会起作用的,再看看上面的段落,你会发现为什么这不会给你想要的效果。在

如果你解决了你的变量问题,你的代码将运行良好(正如我所做的那样)。理解发生这种情况的原因很重要,因为这是一个简单的python。在

当您修复了代码后,如果您的窗口在注册完成后仍保留,则不必担心,您将需要添加另一个方法调用来销毁窗口。在

而{cd6}和

def read():
    addstudent = open ("student.txt", "w")
    addstudent.write("User ID: " + getusername.get())
    addstudent.write("\nUser Password: " + getpassword.get())
    addstudent.close ()

。。。在

^{pr2}$

您没有在代码中创建entry_box1entry_box2,但是您尝试在entry_box1.get()entry_box2.get()中使用它

您必须在两个地方使用getusername而不是entry_box1和{}而不是{}

addstudent.write("Username:" + getusername.get())
addstudent.write("Password: " + getpassword.get())

以及

^{pr2}$

相关问题 更多 >