Home > Software engineering >  'Local variable 'has_mastr_pass' value is not used' in Python
'Local variable 'has_mastr_pass' value is not used' in Python

Time:09-27

I am trying to make a password manager using Tkinter in Python. I keep receiving a warning saying 'Local variable 'has_mastr_pass' value is not used' when I have already used it in an if statement. I am attempting to make it so you have to input a master password before being able to use the rest of the app, but I am having trouble getting the rest of the code to run once I've inputted the master password. I have tried putting global has_mastr_pass before defining the variable, but it didn't work. I'm completely stuck.

Full code:

from tkinter import *

master = ''
passwords = []
has_mastr_pass = True
chars = ['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 master_pass():
   _input = mastr_entry.get()
   # noinspection PyShadowingNames
   has_mastr_pass = True


def enter_value(total_=0):
   input_ = txt.get()
   if input_ != '':
       for i in input_:
           total_  = 1
       if total_ >= 4:
           passwords.append(input_)
           lbl2.configure(text='Password entered.')
       elif total_ < 4:
           lbl2.configure(text='Input 4  digits/letters.')
   else:
       lbl2.configure(text='Input a password.')


def password():
   separator = ", "
   lbl3.configure(text=separator.join(passwords))


window = Tk()
window.geometry('500x300')
window.resizable(False, False)
window.title('Password Manager')
window.iconbitmap('./assets/lock.ico')

mastr_lbl = Label(text='Enter Master Password: ', font=('Times', 20))
mastr_lbl.grid(column=0, row=0)

mastr_entry = Entry(window, width=20)
mastr_entry.grid(column=0, row=1, sticky='w')

mastr_btn = Button(window, text='Enter', command=master_pass, activebackground='grey')
mastr_btn.place(x=126, y=34)

if has_mastr_pass:
   lbl1 = Label(text='Enter Your Password:', font=('Times', 20))
   lbl1.grid(column=0, row=0, sticky='w')

   lbl2 = Label(text='', font=('Times', 15))
   lbl2.grid(column=0, row=2, sticky='w')

   lbl3 = Label(text='', font=('Times', 15))
   lbl3.place(x=0, y=125)

   txt = Entry(window, width=50)
   txt.grid(column=0, row=1)

   btn = Button(window, text='Enter', command=enter_value, activebackground='grey')
   btn.grid(column=1, row=1)

   btn2 = Button(window, text='See Stored Passwords', command=password, activebackground='grey')
   btn2.place(x=0, y=100)

window.mainloop() 

CodePudding user response:

I think it's because you assign a local variable in the function master_pass and then don't do anything with it. You already have a global variable called the same thing but you are not changing it's value inside the master_pass function.

See https://www.w3schools.com/python/python_variables_global.asp

If you wanted to change it in the function then declare it as global in the function. See answer Using global variables in a function

CodePudding user response:

def master_pass():
   _input = mastr_entry.get()
   # noinspection PyShadowingNames
   has_mastr_pass = True

Inside of this function, has_mastr_pass is a local variable that is never used. It is not global.

If you want this function to affect the global variable of the same name, put global has_mastr_pass at the top of the function.

  • Related