简体   繁体   中英

Value of variable (Which is not a tuple) not changing in Tkinter Python

I'm creating a Tkinter program that can generate a random password for you and can store your login information for any website or later use. It generates you a password, asks you for your username(With which you registered), and website name(Where you created an account) so that it can store it. Afterwards, you can search for the login information using this program if you are logging in to the website again. I am trying to add a custom password feature that stores your own custom password instead of storing a random password.

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

In the function custompass(), I try to change the value of x to the user's input and then starting the storeinfo() function to save the information in the.txt file but it does not happen. Instead, it stores the random password instead.

Thank you

Your storeinfo() method always stores x which is a combination of your "random" passwordcharacters. Why does your custompass() always call storeinfo() anyway?

Change the button call to a different method on button press that reads the custom password and provides it to a changed storeinfo method:

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

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

and call it appropriately where need by supplying the thing to store as well.

You could also look into binding functions with parameter to a button: this answer covers it.


From a security standpoint is limiting each n-th character to only one kind of input reducing the quality of the password but I guess its just a "myself" project for now.

You should also look into the string module and random module more closely to make your code better:

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)

Output:

sV6
eF77QS

See:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM