python用户名和密码注册登录_注册用户名和密码

当您编写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()。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值