我用用户名和密码列表创建了一个文本文件。我的程序(在tkinter页面中)应该检查文件中是否存在用户名和密码,如果不存在,则生成一个标签,上面写着“用户名或密码不正确”

‘。但是,即使在文本文件中存在用户名和密码时,它仍然会打印“不正确”的消息。下面是我的文本文件中的一个例子:
testusername.testpassword这是用来检测它的代码:
def login_incorrect():
Label(loginPage, text="Username or password incorrect.").place(x=120, y=120)
# print("def login incorrect")
def LoginToAccount():
print("def login to account")
# while True: # This loop will run as long as the user is not logged in.
with open('AccountDatabase.txt'):
if loginUsernameE.get() + '.' + loginPasswordE.get() not in open('AccountDatabase.txt').read():
login_incorrect()
print('incorrect')
print(loginUsernameE.get() + '.' + loginPasswordE.get())但是,当我在username字段中编写testusername,在password字段中编写testpassword时,它仍然会显示错误。这是一张截图:
为什么我不能检测文本文件中的文本?
发布于 2022-11-27 19:58:57
试试这段代码。我修正了打开文件的读取和条件。
def login_incorrect():
Label(loginPage, text="Username or password incorrect.").place(x=120, y=120)
# print("def login incorrect")
def LoginToAccount():
print("def login to account")
# while True: # This loop will run as long as the user is not logged in.
with open('AccountDatabase.txt', 'r') as f:
if loginUsernameE.get() + '.' + loginPasswordE.get() not in f.read():
login_incorrect()
print('incorrect')
print(loginUsernameE.get() + '.' + loginPasswordE.get())https://stackoverflow.com/questions/74593227
复制相似问题