[英]'Local variable 'has_mastr_pass' value is not used' in Python
I am trying to make a password manager using Tkinter in Python.我正在尝试在 Python 中使用 Tkinter 制作密码管理器。 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.
当我已经在 if 语句中使用它时,我不断收到警告说“未使用局部变量 'has_mastr_pass' 值”。 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.
我正在尝试这样做,因此您必须输入主密码才能使用应用程序的 rest,但是一旦我输入主密码,我无法让代码的 rest 运行。 I have tried putting
global has_mastr_pass
before defining the variable, but it didn't work.我曾尝试在定义变量之前放置
global has_mastr_pass
,但它没有用。 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()
I think it's because you assign a local variable in the function master_pass and then don't do anything with it.我认为这是因为您在 function master_pass 中分配了一个局部变量,然后什么也不做。 You already have a global variable called the same thing but you are not changing it's value inside the master_pass function.
您已经有一个名为相同事物的全局变量,但您没有更改它在 master_pass function 中的值。
See https://www.w3schools.com/python/python_variables_global.asp见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.如果您想在 function 中更改它,请在 function 中将其声明为全局。 See answer Using global variables in a function
请参阅答案在 function 中使用全局变量
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.在这个 function 内部,
has_mastr_pass
是一个从未使用过的局部变量。 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.如果希望这个 function 影响同名的全局变量,请将
global has_mastr_pass
放在 function 的顶部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.