Tkinter Python中未更改的变量值(不是元组)

Tkinter Python中未更改的变量值(不是元组),python,variables,tkinter,Python,Variables,Tkinter,我正在创建一个Tkinter程序,它可以为您生成一个随机密码,并可以存储您的登录信息以供任何网站或以后使用。它会生成一个密码,要求您输入用户名(您注册的用户名)和网站名(您创建帐户的位置),以便存储。之后,如果您再次登录网站,您可以使用此程序搜索登录信息。我正在尝试添加一个自定义密码功能,用于存储您自己的自定义密码,而不是存储随机密码 import tkinter as tk import random window = tk.Tk() window.title("Password gen

我正在创建一个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()
将tkinter作为tk导入
随机输入
window=tk.tk()
窗口标题(“密码生成器”)#窗口标题
窗口。几何形状(“450x300”)#窗口尺寸
#创建密码的随机字符
一=['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'、'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']
三=['1','2','3','4','5','6','7','8','9','0']
四=[“~”、“@”、“!”、“#”、“$”、“%”、“^”、“&”、“*”、“(“,”)、“-”、“"”、“=”、“+”、“:”、“;”、“、”、“/”、“?”
五=['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']
六=['1'、'2'、'3'、'4'、'5'、'6'、'7'、'8'、'9'、'0']
七=[“~”、“@”、“!”、“#”、“$”、“%”、“^”、“&”、“*”、“(”、“)”、“-”、“"”、“=”、“+”、“:”、“;”、“、”、“/”、“?”
八个=[“~”、“@”、“!”、“#”、“$”、“%”、“^”、“&”、“*”、“(”、“)”、“-”、“"”、“=”、“+”、“:”、“;”、“、”、“/”、“?”
九=['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'、'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=1[random.randint(0,25)]
b=2[random.randint(0,25)]
c=3[random.randint(0,9)]
d=4[random.randint(0,22)]
e=5[random.randint(0,25)]
f=6[random.randint(0,9)]
g=7[random.randint(0,22)]
h=8[random.randint(0,22)]
i=9[random.randint(0,25)]
j=10[random.randint(0,25)]
x=a+b+c+d+e+f+g+h+i+j#密码(包括各种字符)
Generate=tk.Label(text=“这是您随机生成的密码:”)
生成.grid(列=3,行=3)
UserName=tk.StringVar()#创建包含变量的用户名和网站
WebSite=tk.StringVar()
passw=tk.Label(text=x)#显示密码
通过网格(列=3,行=4)
UserN=tk.Label(text=“用户名/电子邮件”)
UserN.grid(列=2,行=5)
username=tk.Entry(window,width=30,textvariable=username)#输入用户名并存储它
username.grid(列=3,行=5)
WebS=tk.Label(text=“Website Name”)#输入网站名称并存储
网格(列=2,行=6)
website=tk.Entry(window,width=30,textvariable=website)#输入网站名称并存储
网格(列=3,行=6)
def storeinfo():#将信息存储在.txt文件中
打开(“LoginInfo.txt”、“a”)作为登录信息:
logininfo.write(“%s,%s,%s\n%”(WebSite.get(),UserName.get(),x))
savesuccess=tk.Label(text=“Save successful!”,fg=“Purple”)
savesuccess.grid(列=4,行=8)
save=tk.按钮(text=“save Info”,command=storeinfo)#执行命令的按钮
save.grid(列=3,行=8)
#搜索您的登录信息
searchentry=tk.StringVar()#存储用户的条目(仅限网站名称)
searchent=tk.Entry(textvariable=searchentry)#输入网站名称
网格(列=3,行=11)
def search():#用于搜索的命令
以open(“LoginInfo.txt”)作为fo:
对于fo中的rec:
tokens=rec.strip().split(',',2)#最多拆分为三个令牌
if tokens[0]==searchentry.get():#如果网站名称(位于列表的第一个)等于searchentry
searches=tk.Label(text=rec)#给出整个列表
searches.grid(列=3,行=12)
searchbutton=tk.按钮(宽度=10,text=“search”,command=search)#执行搜索命令的按钮
searchbutton.grid(列=4,行=11)
def custompass():#如果用户已经有密码并且只想存储它
anc=tk.StringVar()
custompass1=tk.Entry(text=“添加自定义密码”,textvariable=x)#将自定义密码存储在x中
custompass1.grid(列=3,行=15)
custompasslabel1=tk.Label(text=“Password:”)
custompasslabel1.grid(列=2,行=15)
custompass2=tk.Entry(text=“添加用户名/电子邮件”,textvariable=Username)#将用户名存储在Username中
custompass2.grid(列=3,行=16)
custompasslabel2=tk.Label(text=“用户名/电子邮件:”)
custompasslabel2.grid(列=2,行=16)
custompass3=tk.Entry(text=“添加网站名称”,textvariable=website)#将网站名称存储在网站中
custompass3.grid(列=3,行=17)
custompasslabel1=tk.Label(text=“网站:”)
custompasslabel1.grid(列=2,行=17)
submitbutton=tk.Button(text=“Submit”,command=storeinfo)#执行storeinfo命令的按钮
网格(列=3,行=18)
storeinfo()
custompassb=tk.Button(text=“添加自定义密码”,command=custompass)#启动custompass命令的按钮
custompassb.grid(列=3,行=14)
window.mainloop()
在custompass()函数中,我尝试将x的值更改为用户的输入,然后启动storeinfo()函数将信息保存在.txt文件中,但没有发生。相反,它存储的是随机密码

谢谢你

你的
storeinfo()
方法总是存储
x
,它是你的“随机”密码字符的组合。为什么您的
custompass()
总是调用
storeinfo()

将按钮调用更改为按钮按下时读取自定义密码并将其提供给更改后的
storeinfo
方法的其他方法:

def storeCustomPw(): # use that from your custom function
    storeinfo(custompass1.get()) 
修改存储信息:

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)
并在需要的地方适当地调用它,同时提供要存储的东西

您还可以研究将参数绑定到按钮的函数:这个cov
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