我正在创建一个Tkinter程序,该程序可以为您生成一个随机密码,并可以将您的登录信息存储到任何网站或以后使用。它会为您生成一个密码,询问您的用户名(注册时使用的用户名)和网站名称(您在其中创建帐户的位置)以便可以存储它。之后,如果您再次登录网站,则可以使用该程序搜索登录信息。我试图添加一个自定义密码功能,该功能存储您自己的自定义密码,而不是存储随机密码。
import tkinter as tk
import random
window = tk.Tk()
window.title("Password generator") #Window title
window.geometry("450x300") #Window Size
#Random characters to create a password
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 #Password(Includes various characters)
Generate = tk.Label(text="Here is your randomly generated password:")
Generate.grid(column=3, row=3)
UserName = tk.StringVar() #Make Username and website containing variables
WebSite = tk.StringVar()
passw = tk.Label(text=x) #Display the password
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) #Take in the username and store it
username.grid(column=3, row=5)
WebS = tk.Label(text="Website Name") #Take in the website name and store it
WebS.grid(column=2, row=6)
website = tk.Entry(window, width=30, textvariable=WebSite) #Take in the website name and store it
website.grid(column=3, row=6)
def storeinfo(): #Store the information in a .txt file
with open("LoginInfo.txt", "a") as logininfo:
logininfo.write('%s,%s,%s\n' % (WebSite.get(), UserName.get(), x))
savesuccess = tk.Label(text="Save successful!", fg="Purple")
savesuccess.grid(column=4, row=8)
save = tk.Button(text="Save Info", command=storeinfo) #Button to execute command
save.grid(column=3, row=8)
#Search for your login info
searchentry = tk.StringVar() #Store the user's entry(The website name only)
searchent = tk.Entry(textvariable=searchentry) #Take in the website name
searchent.grid(column=3, row=11)
def search(): #Command to 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(): #If the website name(which is first in the list)is equal to searchentry
searches = tk.Label(text=rec) #Give out the entire list
searches.grid(column=3, row=12)
searchbutton = tk.Button( width=10, text="search", command=search) #Button to execute search command
searchbutton.grid(column=4, row=11)
def custompass(): #If the user already has a password and just wishes to just store it
anc = tk.StringVar()
custompass1 = tk.Entry(text="Add a custom password", textvariable=x) #Store the custom password in x
custompass1.grid(column=3, row=15)
custompasslabel1 = tk.Label(text="Password: ")
custompasslabel1.grid(column=2, row=15)
custompass2 = tk.Entry(text="Add Username/Email", textvariable=UserName) #Store the username in UserName
custompass2.grid(column=3, row=16)
custompasslabel2 = tk.Label(text="Username/Email: ")
custompasslabel2.grid(column=2, row=16)
custompass3 = tk.Entry(text="Add website name", textvariable=WebSite) #Store the website name in WebSite
custompass3.grid(column=3, row=17)
custompasslabel1 = tk.Label(text="Website: ")
custompasslabel1.grid(column=2, row=17)
submitbutton = tk.Button(text="Submit", command=storeinfo) #Button to execute storeinfo command
submitbutton.grid(column=3, row=18)
storeinfo()
custompassb = tk.Button(text="Add a custom password", command=custompass) #Button to start the custompass command
custompassb.grid(column=3, row=14)
window.mainloop()
在custompass()函数中,我尝试将x的值更改为用户的输入,然后启动storeinfo()函数以将信息保存在.txt文件中,但不会发生。而是存储随机密码。
谢谢
您的storeinfo()
方法不存储x
,这是您的“随机”密码字符的组合。
给它存储一个参数:
def storeinfo(pw = None):
# store x if no pw was given
if pw is None:
pw = x
with open("LoginInfo.txt", "a") as logininfo:
logininfo.write('%s,%s,%s\n' % (WebSite.get(), UserName.get(), pw)) # save pw
savesuccess = tk.Label(text="Save successful!", fg="Purple")
savesuccess.grid(column=4, row=8)
并且还通过提供要存储的东西适当地调用它。
您还应该更仔细地研究string
模块和random
模块,以使您的代码更好:
from string import ascii_lowercase, ascii_uppercase, digits
a = random.choice(ascii_lowercase)
b = random.choice(ascii_uppercase)
c = random.choice(digits)
# etc. There are other constants for other kinds of things
[从安全的角度来看,将每个第n个字符限制为仅一种输入,这会降低密码的质量,但我想目前它只是一个“我自己”项目。
您可以做类似的事情:
随机导入从字符串导入ascii_lowercase,ascii_uppercase,数字
a = random.choice(ascii_lowercase)
b = random.choice(ascii_uppercase)
c = random.choice(digits)
print(a+b+c)
# or use loops of "sources" and combine them like so
source = [ascii_lowercase, ascii_uppercase, digits, digits, ascii_uppercase, ascii_uppercase]
pw = ''.join( random.choice(w) for w in source )
print(pw)
输出:
sV6
eF77QS