0

I am writing a password generation app, with multiple frames, one to generate a psudo-random password and another to save it. But I keep on encountering this bug "NameError: name 'txtfin' is not defined" (on line that executes txtpass.insert('1.0', txtfin)), when I try to pass the password (txtfin) from one frame to another.

I can't figure out why. I tried it with/ without the global keyword, I tried to put it outside of the function block, I tried it with and without the return txtfin, I tried to move it around (put it at the top under the #Widgets section). I even tried to move the function definition around. I don't know what else to try, but most importantly I don't understand why it doesn't recognize txtfin as defined. It's almost like txtfin is just not seen by the rest of the program even though I declared it as a global keyword.

Btw I know that using global variables is discouraged, but I am just experimenting and nothing else I tried seemed to work. Like for example the other way I tried it, was to declare txtfin outside of the function, pass it to the function as a variable and return it, but when I do that I encounter another problem, that's because I have to assign pick(txtfin) to a variable (in order to display that string later on), but the function gets called as a command in the getBtn button, so that way leads nowhere.

Any help or nudge in the right direction would be appreciated. I've been stuck on this problem for a while. Please take it easy on me I am fairly novice.

#Widgets:
from tkinter import *
from random import sample

def show_frame(frame):
    frame.tkraise()

window = Tk()
window.geometry("375x108")

img = PhotoImage(file='logo.gif')

#-------------------Frame code--------------------#
frame1=Frame(window)
frame2=Frame(window)

for frame in (frame1, frame2):
    frame.grid(row=0, column=0, sticky='nsew')

#--------------------Frame 1 code--------------------#
#Password code
imgLbl = Label(frame1, image=img)
txt = Text(frame1, width=29, height = 1)
getBtn = Button(frame1)
resBtn = Button(frame1)
frame1_btn = Button(frame1, text='Save', command=lambda:show_frame(frame2))
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)
frame1_btn.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']

def pick():
    passlen = sample(range(12,16),1)
    plnum = passlen[0]
    
    nums = sample(range(0,62),plnum)
    
    global txtfin
    txtfin = ''
    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())
resBtn.configure(command=reset)

txt2.insert('1.0',txtfin)

#--------------------Frame 2 code--------------------#
imgLbl = Label(frame2, image=img)
label1 = Label(frame2, relief='groove', width=8)
label2 = Label(frame2, relief='groove', width=8)
label3 = Label(frame2, relief='groove', width=8)
txtname = Text(frame2, width=22, height = 1)
txtuser = Text(frame2, width=22, height = 1)
txtpass = Text(frame2, width=22, height = 1)
back_btn = Button(frame2, text='Back', command=lambda:show_frame(frame1))
getBtn = Button(frame2)
saveBtn = Button(frame2)

#Geometry:
imgLbl.place(x = 0, y = 0)
label1.place(x = 118, y = 5)
label2.place(x = 118, y = 30)
label3.place(x = 118, y = 55)
txtname.place(x = 200, y = 5)
txtuser.place(x = 200, y = 30)
txtpass.place(x = 200, y = 55)
getBtn.place(x = 118, y = 79)
saveBtn.place(x = 271, y = 79)
back_btn.place(x = 310, y = 79)

#Static Properties:
window.title('Save A New Password')
window.resizable(0,0)
getBtn.configure(text = 'Generate A New Password')
saveBtn.configure(text = 'Save')

#Initial Properties:
label1.configure(text='Name')
label2.configure(text='Username')
label3.configure(text='Password')
#resBtn.configure(state=DISABLED)


#Dynamic Properties
txtname.insert('1.0','Google')
txtuser.insert('1.0','screen name')
txtpass.insert('1.0', txtfin)


show_frame(frame1)

#Sustain window:
window.mainloop()
max d
  • 21
  • 1
  • 1
  • 4
  • 1
    Please post the full error traceback. – ewokx Nov 10 '20 at 06:16
  • Python has scopes for variables, if you leave the scope the variable is no longer known. – Patrick Artner Nov 10 '20 at 06:21
  • `import string` - `alpha = list(string.ascii_letters + string.ascii_digits)` – Patrick Artner Nov 10 '20 at 06:22
  • In your case: you declare the usage of `pick()` in a lambda (which does not run it) then the code proceeds to try to insert the `global txtfin` - which was not yet created because the function that declares it has not been run yet. So: scoping. either declare a dummy `txtfin` or run pick() so the variable declared global in that scope takes effect – Patrick Artner Nov 10 '20 at 06:28
  • So to sum up, as long as `pick()` is not run, the variable inside it remains undefined. So you will have to run it to define the variables inside `pick()` and get access to them(using `global`). And also i would recommend to create a new window instead of creating a new frame to show new contents. – Delrius Euphoria Nov 10 '20 at 06:38

0 Answers0