首页 > 解决方案 > 变量的值(不是元组)在 Tkinter Python 中没有变化

问题描述

我正在创建一个 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 文件中,但没有发生。相反,它存储随机密码。

谢谢

标签: pythonvariablestkinter

解决方案


您的storeinfo()方法始终存储x您的“随机”密码字符的组合。为什么你custompass()总是打电话storeinfo()

在按钮按下时将按钮调用更改为不同的方法,该方法读取自定义密码并将其提供给更改的storeinfo方法:

def storeCustomPw(): # use that from your custom function
    storeinfo(custompass1.get()) 

修改storeinfo:

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)

并通过提供要存储的东西在需要的地方适当地调用它。

您还可以查看带有按钮参数的绑定函数:这个答案涵盖了它。


从安全的角度来看,将每个第 n 个字符限制为仅一种输入,从而降低了密码的质量,但我想它现在只是一个“我自己”的项目。

您还应该更仔细地研究string模块和random模块,以使您的代码更好:

import random
from string import ascii_lowercase, ascii_uppercase, digits

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

看:


推荐阅读