Search code examples
pythontkintersearchbardata-extraction

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


I am quite new to Python, rather programming as a whole and I have been creating a password generator and login info storage program which will generate you a password and give you the option of storing your username and password in a .txt file. I am trying to create a search bar that extracts the data from a .txt file and displays the results but it is not working:

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()

If you run the code, you will not be able to search for previously entered data. I am not able to understand the cause.

Thanks


Solution

  • You should save the login information in the following format: website,username, password:

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

    Then you read the file line by line, split the line into tokens and compare searchentry.get() with the first token:

    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)
    

    Note: if more than one record are returned, only the last record will be visible as you grid the result into same grid cell.