我正在創建一個注冊登錄,其中資料保存在 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檔案在同一行嗎?
uj5u.com熱心網友回復:
@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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/425284.html
