如何添加从 .txt 文件中获取信息并将其显示在 tkinter 中的搜索栏

How to add a search bar which takes info from a .txt file and displays it in tkinter

我是 Python 的新手,而不是整体编程,我一直在创建一个密码生成器和登录信息存储程序,它将为您生成一个密码,并让您可以选择存储您的用户名和.txt 文件中的密码。我正在尝试创建一个搜索栏,从 .txt 文件中提取数据并显示结果,但它不起作用:

import tkinter as tk 
import random

window = tk.Tk()

window.title("Password generator")
window.geometry("500x500")


one = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
two = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
three = ['1','2','3','4','5','6','7','8','9','0']
four = ['~','@','!','#','$','%','^','&','*','(',')','-','_','=','+',':',';',',','.','<','>','/','?']
five = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
six = ['1','2','3','4','5','6','7','8','9','0']
seven = ['~','@','!','#','$','%','^','&','*','(',')','-','_','=','+',':',';',',','.','<','>','/','?']
eight = ['~','@','!','#','$','%','^','&','*','(',')','-','_','=','+',':',';',',','.','<','>','/','?']
nine = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
ten = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

a = one[random.randint(0, 25)]
b = two[random.randint(0, 25)]
c = three[random.randint(0, 9)]
d = four[random.randint(0, 22)]
e = five[random.randint(0, 25)]
f = six[random.randint(0, 9)]
g = seven[random.randint(0, 22)]
h = eight[random.randint(0, 22)]
i = nine[random.randint(0, 25)]
j = ten[random.randint(0, 25)]

x = a+b+c+d+e+f+g+h+i+j


Generate = tk.Label(text="Here is your randomly generated password:") 
Generate.grid(column=3, row=3)

UserName = tk.StringVar()
WebSite = tk.StringVar()

passw = tk.Label(text=x) 
passw.grid(column=3, row=4)

UserN = tk.Label(text="Username/Email") 
UserN.grid(column=2, row=5)

username = tk.Entry(window, width=30, textvariable=UserName)
username.grid(column=3, row=5)

WebS = tk.Label(text="Website Name")
WebS.grid(column=2, row=6)

website = tk.Entry(window, width=30, textvariable=WebSite)
website.grid(column=3, row=6)

def storeinfo():
    logininfo = open("LoginInfo.txt", "a")
    logininfo.write(x+UserName.get()+WebSite.get())

save = tk.Button(text="Save Info", command=storeinfo)
save.grid(column=3, row=8)

searchentry = tk.StringVar()

searchent = tk.Entry(textvariable=searchentry)
searchent.grid(column=3, row=10)



def search():
    with open("LoginInfo.txt") as fo:
        for rec in fo:
            if rec == searchentry.get():
                searches = tk.Label(text=rec)
                searches.grid(column=3, row=11)

searchbutton = tk.Button( width=10, text="search", command=search)
searchbutton.grid(column=4, row=10)


window.mainloop()

如果您 运行 代码,您将无法搜索以前输入的数据。我无法理解原因。

谢谢

您应按以下格式保存登录信息:网址、用户名、密码:

def storeinfo():
    with open("LoginInfo.txt", "a") as logininfo:
        logininfo.write('%s,%s,%s\n' % (WebSite.get(), UserName.get(), x))

然后逐行读取文件,将行拆分为标记并将 searchentry.get() 与第一个标记进行比较:

def search():
    with open("LoginInfo.txt") as fo:
        for rec in fo:
            tokens = rec.strip().split(',', 2) # split to maximum three tokens
            if tokens[0] == searchentry.get():
                searches = tk.Label(text=rec)
                searches.grid(column=3, row=11)

注意:如果返回多条记录,当您将结果网格化到同一个网格单元格时,只有最后一条记录可见。