如何在 tkinter 中从文件读取数据创建密码系统
How to create password system in tkinter reading the data from file
我想在 tkinter 中创建密码系统,记事本作为我的数据库,其中包含我工作目录中的数据,但是当我在输入字段中插入数据时,我收到错误登录 failed.Have 创建了 txt 文件但是该函数似乎无法从 file.Any 关于如何执行此操作的建议中读取。
import tkinter as tk
import sys
from tkinter import messagebox
now = open("passdoc.txt","w+")
now.write("user\n")
now.write("python3")
now.close()
def login_in():
with open("passdoc.txt") as f:
new = f.readlines()
name = new[0].rstrip()
password = new[1].rstrip()
if entry1.get() == new[0] in passdoc.txt and entry2.get() == new[1] in
passdoc.txt:
root.deiconify()
log.destroy()
else:
messagebox.showerror("error","login Failed")
def close():
log.destroy() #Removes toplevel window
root.destroy() #Removes root window
sys.exit() #Ends the script
root=tk.Tk()
log = tk.Toplevel() #
root.geometry("350x350")
log.geometry("200x200")
entry1 = tk.Entry(log) #Username entry
entry2 = tk.Entry(log) #Password entry
button1 = tk.Button(log, text="Login", command=login_in) #Login button
button2 = tk.Button(log, text="Cancel", command=close) #Cancel button
label1 = tk.Label(root, text="tkinter password system")
entry1.pack()
entry2.pack()
button1.pack()
button2.pack()
label1.place(x=30,y=300)
label = tk.Label(root, text="welcome").pack()
root.withdraw()
root.mainloop()
我也创建了这个函数,但似乎对我来说并不奏效
def login_in():
with open("passdoc.txt") as f:
new = f.readlines()
name = new[0].rstrip()
password = new[1].rstrip()
if entry1.get() == name in passdoc.txt and entry2.get() == password in
passdoc.txt:
root.deiconify()
log.destroy()
else:
messagebox.showerror("errror","login failed") #error login failed
(corrections)
您的代码中有一些内容需要改进,但主要问题出在您的 login_in()
函数中。
你的if
说法全错了。我不确定你为什么这样写,但让我们修复它。
因为您定义了 name = new[0].rstrip()
和 password = new[1].rstrip()
您可以使用 name
和 password
来验证用户是否输入了正确的凭据。
因此您的 if
语句应如下所示:
if entry1.get() == name and entry2.get() == password:
您对 in passdoc.txt
的使用没有任何作用,也无法工作,因为 passdoc.txt
到 python 看起来像一个未定义的变量,并且会在 if 语句中失败。请记住,您将 passdoc.txt
的所有内容导入为 f
,因此您没有创建名为 passdoc
的变量,而是为 with open()
语句创建了一个名为 f
的变量
如果你想稍微缩短你的代码,你可以删除 name
和 password
变量,然后只用 new
编写 if 语句
因此您的 login_in()
函数可能如下所示:
def login_in():
with open("passdoc.txt") as f:
new = f.readlines()
name = new[0].rstrip()
password = new[1].rstrip()
if entry1.get() == name and entry2.get() == password:
root.deiconify()
log.destroy()
else:
messagebox.showerror("error","login Failed")
或者这个:
def login_in():
with open("passdoc.txt") as f:
new = f.readlines()
if entry1.get() == new[0].rstrip() and entry2.get() == new[1].rstrip():
root.deiconify()
log.destroy()
else:
messagebox.showerror("error","login Failed")
我想在 tkinter 中创建密码系统,记事本作为我的数据库,其中包含我工作目录中的数据,但是当我在输入字段中插入数据时,我收到错误登录 failed.Have 创建了 txt 文件但是该函数似乎无法从 file.Any 关于如何执行此操作的建议中读取。
import tkinter as tk
import sys
from tkinter import messagebox
now = open("passdoc.txt","w+")
now.write("user\n")
now.write("python3")
now.close()
def login_in():
with open("passdoc.txt") as f:
new = f.readlines()
name = new[0].rstrip()
password = new[1].rstrip()
if entry1.get() == new[0] in passdoc.txt and entry2.get() == new[1] in
passdoc.txt:
root.deiconify()
log.destroy()
else:
messagebox.showerror("error","login Failed")
def close():
log.destroy() #Removes toplevel window
root.destroy() #Removes root window
sys.exit() #Ends the script
root=tk.Tk()
log = tk.Toplevel() #
root.geometry("350x350")
log.geometry("200x200")
entry1 = tk.Entry(log) #Username entry
entry2 = tk.Entry(log) #Password entry
button1 = tk.Button(log, text="Login", command=login_in) #Login button
button2 = tk.Button(log, text="Cancel", command=close) #Cancel button
label1 = tk.Label(root, text="tkinter password system")
entry1.pack()
entry2.pack()
button1.pack()
button2.pack()
label1.place(x=30,y=300)
label = tk.Label(root, text="welcome").pack()
root.withdraw()
root.mainloop()
我也创建了这个函数,但似乎对我来说并不奏效
def login_in():
with open("passdoc.txt") as f:
new = f.readlines()
name = new[0].rstrip()
password = new[1].rstrip()
if entry1.get() == name in passdoc.txt and entry2.get() == password in
passdoc.txt:
root.deiconify()
log.destroy()
else:
messagebox.showerror("errror","login failed") #error login failed
(corrections)
您的代码中有一些内容需要改进,但主要问题出在您的 login_in()
函数中。
你的if
说法全错了。我不确定你为什么这样写,但让我们修复它。
因为您定义了 name = new[0].rstrip()
和 password = new[1].rstrip()
您可以使用 name
和 password
来验证用户是否输入了正确的凭据。
因此您的 if
语句应如下所示:
if entry1.get() == name and entry2.get() == password:
您对 in passdoc.txt
的使用没有任何作用,也无法工作,因为 passdoc.txt
到 python 看起来像一个未定义的变量,并且会在 if 语句中失败。请记住,您将 passdoc.txt
的所有内容导入为 f
,因此您没有创建名为 passdoc
的变量,而是为 with open()
语句创建了一个名为 f
的变量
如果你想稍微缩短你的代码,你可以删除 name
和 password
变量,然后只用 new
因此您的 login_in()
函数可能如下所示:
def login_in():
with open("passdoc.txt") as f:
new = f.readlines()
name = new[0].rstrip()
password = new[1].rstrip()
if entry1.get() == name and entry2.get() == password:
root.deiconify()
log.destroy()
else:
messagebox.showerror("error","login Failed")
或者这个:
def login_in():
with open("passdoc.txt") as f:
new = f.readlines()
if entry1.get() == new[0].rstrip() and entry2.get() == new[1].rstrip():
root.deiconify()
log.destroy()
else:
messagebox.showerror("error","login Failed")