我正在编写一个密码生成应用程序,该应用程序具有多个框架,一个框架生成一个伪随机密码,另一个框架显示并保存它。但是我无法弄清楚在按下pick()
时如何分配getBtn
的返回值,因此以后可以在txt2.insert('1.0',txtfin)
中显示它,因为据我所知您必须将pick(txtfin)
分配给某些内容(例如newvar = pick(txtfin)
)以包含返回的值。事实是,密码生成工作正常,并显示在txt.insert('1.0',txtfin)
中,但这只能起作用,因为它位于pick()
函数内部。我需要将返回的pick()
值分配给一个变量的原因是因为我想稍后将其显示在另一个框架中。
任何朝着正确方向的帮助或推动将不胜感激。我已经在这个问题上停留了一段时间。如果我做错了什么,请放心,我是新手。
from tkinter import *
from random import sample
def show_frame(frame):
frame.tkraise()
window = Tk()
window.geometry("375x108")
img = PhotoImage(file='logo.gif')
frame1=Frame(window)
frame1.grid(row=0, column=0, sticky='nsew')
#Password code
imgLbl = Label(frame1, image=img)
txt = Text(frame1, width=29, height = 1)
getBtn = Button(frame1)
resBtn = Button(frame1)
txt2 = Text(frame1, width=20, height = 1)
#Geometry:
imgLbl.pack(side = LEFT)
txt.place(x = 122, y = 15)
getBtn.pack(side = LEFT, padx = 5, pady = 15)
resBtn.pack(side = LEFT, padx = 5, pady = 15)
txt2.place(x = 122, y = 70)
#Static Properties:
window.title('Random Password Generator')
window.resizable(0,0)
getBtn.configure(text = 'Generate A New Password')
resBtn.configure(text = 'Reset')
#Initial Properties:
resBtn.configure(state=DISABLED)
#Dynamic Properties:
alpha = ['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','0','1','2','3','4','5','6','7','8','9']
txtfin = ''
def pick(txtfin):
passlen = sample(range(12,16),1)
plnum = passlen[0]
nums = sample(range(0,62),plnum)
for i in nums:
txtfin = txtfin + alpha[i]
txt.insert('1.0',txtfin)
getBtn.configure(state=DISABLED)
resBtn.configure(state=NORMAL)
return txtfin
def reset():
txt.delete('1.0', END)
getBtn.configure(state=NORMAL)
resBtn.configure(state=DISABLED)
getBtn.configure(command=lambda:pick(txtfin))
resBtn.configure(command=reset)
txt2.insert('1.0',txtfin)
show_frame(frame1)
#Sustain window:
window.mainloop()