读取名称和密码是否在 txt 文件中的同一行
read if name and password are in the same line in a txt file
我正在创建一个注册登录,其中数据保存在一个 txt 文件中,还有一个登录。
但现在如果我使用不匹配的名称和密码登录,我仍然登录。
在txt文件中只显示用户名( , )和注册时输入的密码
这是代码:
from tkinter import *
import numpy as np
import ast
root = Tk()
root.geometry("500x300")
root.title("form")
def signup():
# verificare se un utente gia esiste
registro_fatto.configure(text="Completata")
file1 = open("provafile.txt", "a")
file1.write(nome_utente_entry.get())
file1.write(", ")
file1.write(password_entry.get())
file1.write("\n")
file1.close()
nome_utente_entry.delete(0, END)
password_entry.delete(0, END)
def signin():
global nome_utente_entry
global password_entry
file = open('provafile.txt', 'r')
file.readlines()
file.close()
if nome_utente_entry.get() and password_entry.get() in file:
accesso_fatto.configure(text="accesso eseguito")
elif nome_utente_entry.get() and password_entry.get() != file:
accesso_fatto.configure(text="accesso negato")
else:
accesso_fatto.configure(text="Nome utente e password negato")
nome_utente_entry.delete(0, END)
password_entry.delete(0, END)
#input nome utente
nome_utente = Label(root, text="Nome", font="Times 10 bold")
nome_utente.grid(row=1, column=1, padx=60)
nome_utente_entry = Entry(root)
nome_utente_entry.grid(row=1, column=2)
#input password
password = Label(root, text="Password", font="Times 10 bold")
password.grid(row=2, column=1)
password_entry = Entry(root, show="*")
password_entry.grid(row=2, column=2)
#bottone per accedere
accedi_button = Button(root, text="accedi", command=signin)
accedi_button.grid(row=3, column=1)
accedi_button.configure(cursor="hand2")
accesso_fatto = Label(root, text="", font="Times 10 bold")
accesso_fatto.grid(row=4, column=1)
#bottone per registrarsi
registrati_button = Button(root, text="registrati", command=signup)
registrati_button.grid(row=3, column=2)
registrati_button.configure(cursor="hand2")
registro_fatto = Label(root, text="", font="Times 10 bold")
registro_fatto.grid(row=4, column=2)
if __name__ == '__main__':
root.mainloop()
拜托你能告诉我如何检查用户名和密码是否与txt文件在同一行吗?
@matteopet,你可以使用这行代码来解决,但是你必须改进它才能进一步解决。将两个函数更改为:
def signup():
# verificare se un utente gia esiste
if (nome_utente_entry.get() != '' and password_entry.get() != ''):
registro_fatto.configure(text="Completata")
file1 = open("provafile.txt", "a")
file1.write(nome_utente_entry.get())
file1.write(", ")
file1.write(password_entry.get())
file1.write("\n")
file1.close()
nome_utente_entry.delete(0, END)
password_entry.delete(0, END)
else:
registro_fatto.configure(text="Provide data")
def signin():
global nome_utente_entry
global password_entry
file = open('provafile.txt', 'r')
match = 0
if(nome_utente_entry.get() == '' or password_entry.get() == ''):
accesso_fatto.configure(text="Nome utente e password negato")
else:
for line in file.readlines():
nome, password = line.strip().split(",")
if(nome.strip() == nome_utente_entry.get() and password.strip() == password_entry.get()):
match +=1
break
else:
continue
if match == 1:
accesso_fatto.configure(text="accesso eseguito")
elif match == 0:
accesso_fatto.configure(text="accesso negato")
nome_utente_entry.delete(0, END)
password_entry.delete(0, END)
file.close()
我正在创建一个注册登录,其中数据保存在一个 txt 文件中,还有一个登录。 但现在如果我使用不匹配的名称和密码登录,我仍然登录。
在txt文件中只显示用户名( , )和注册时输入的密码
这是代码:
from tkinter import *
import numpy as np
import ast
root = Tk()
root.geometry("500x300")
root.title("form")
def signup():
# verificare se un utente gia esiste
registro_fatto.configure(text="Completata")
file1 = open("provafile.txt", "a")
file1.write(nome_utente_entry.get())
file1.write(", ")
file1.write(password_entry.get())
file1.write("\n")
file1.close()
nome_utente_entry.delete(0, END)
password_entry.delete(0, END)
def signin():
global nome_utente_entry
global password_entry
file = open('provafile.txt', 'r')
file.readlines()
file.close()
if nome_utente_entry.get() and password_entry.get() in file:
accesso_fatto.configure(text="accesso eseguito")
elif nome_utente_entry.get() and password_entry.get() != file:
accesso_fatto.configure(text="accesso negato")
else:
accesso_fatto.configure(text="Nome utente e password negato")
nome_utente_entry.delete(0, END)
password_entry.delete(0, END)
#input nome utente
nome_utente = Label(root, text="Nome", font="Times 10 bold")
nome_utente.grid(row=1, column=1, padx=60)
nome_utente_entry = Entry(root)
nome_utente_entry.grid(row=1, column=2)
#input password
password = Label(root, text="Password", font="Times 10 bold")
password.grid(row=2, column=1)
password_entry = Entry(root, show="*")
password_entry.grid(row=2, column=2)
#bottone per accedere
accedi_button = Button(root, text="accedi", command=signin)
accedi_button.grid(row=3, column=1)
accedi_button.configure(cursor="hand2")
accesso_fatto = Label(root, text="", font="Times 10 bold")
accesso_fatto.grid(row=4, column=1)
#bottone per registrarsi
registrati_button = Button(root, text="registrati", command=signup)
registrati_button.grid(row=3, column=2)
registrati_button.configure(cursor="hand2")
registro_fatto = Label(root, text="", font="Times 10 bold")
registro_fatto.grid(row=4, column=2)
if __name__ == '__main__':
root.mainloop()
拜托你能告诉我如何检查用户名和密码是否与txt文件在同一行吗?
@matteopet,你可以使用这行代码来解决,但是你必须改进它才能进一步解决。将两个函数更改为:
def signup():
# verificare se un utente gia esiste
if (nome_utente_entry.get() != '' and password_entry.get() != ''):
registro_fatto.configure(text="Completata")
file1 = open("provafile.txt", "a")
file1.write(nome_utente_entry.get())
file1.write(", ")
file1.write(password_entry.get())
file1.write("\n")
file1.close()
nome_utente_entry.delete(0, END)
password_entry.delete(0, END)
else:
registro_fatto.configure(text="Provide data")
def signin():
global nome_utente_entry
global password_entry
file = open('provafile.txt', 'r')
match = 0
if(nome_utente_entry.get() == '' or password_entry.get() == ''):
accesso_fatto.configure(text="Nome utente e password negato")
else:
for line in file.readlines():
nome, password = line.strip().split(",")
if(nome.strip() == nome_utente_entry.get() and password.strip() == password_entry.get()):
match +=1
break
else:
continue
if match == 1:
accesso_fatto.configure(text="accesso eseguito")
elif match == 0:
accesso_fatto.configure(text="accesso negato")
nome_utente_entry.delete(0, END)
password_entry.delete(0, END)
file.close()