Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python局部变量“charcheck”在赋值之前被引用,为什么?_Python_Python 3.x_Python 2.7_Tkinter_Tkinter Entry - Fatal编程技术网

Python局部变量“charcheck”在赋值之前被引用,为什么?

Python局部变量“charcheck”在赋值之前被引用,为什么?,python,python-3.x,python-2.7,tkinter,tkinter-entry,Python,Python 3.x,Python 2.7,Tkinter,Tkinter Entry,我正在尝试使用python tkinter编写一个密码验证程序应用程序。 如果插入的密码包含至少2个数字、至少2个特殊字符和至少7个长度,则认为该密码为强密码,否则为弱密码 如果我输入弱密码,程序将工作,但如果我输入强密码,我将面临此错误: Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python3.8/tkinter/__init__.py", line

我正在尝试使用python tkinter编写一个密码验证程序应用程序。 如果插入的密码包含至少2个数字、至少2个特殊字符和至少7个长度,则认为该密码为强密码,否则为弱密码

如果我输入弱密码,程序将工作,但如果我输入强密码,我将面临此错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
    return self.func(*args)
  File "/home/liana/projects/python/modification/modify.py", line 15, in submit
    charcheck += 1
UnboundLocalError: local variable 'charcheck' referenced before assignment
我不知道为什么

这是我的密码:

import tkinter as tk 

numcheck = 0
charcheck = 0

root=tk.Tk()  
root.geometry("600x400")  
passw_var=tk.StringVar() 

def submit(): 
    password=passw_var.get()    
    passw_var.set("")
    for i in range(len(password)):
        if(password[i]=='!' or password[i]=='@' or password[i]=='#' or password[i]=='$' or password[i]=='&' or password[i]=='%' or password[i]=='*'):
            charcheck += 1

        elif (ord(password[i])>=48 and ord(password[i])<=57):
            numcheck += 1 

    if (len(password)>=7 and charcheck>=2 and numcheck>=2):
        result_label = tk.Label(root, text='STRONG', font=('calibre',10, 'bold')).grid(row=3, column=2)

    else:
        result_label = tk.Label(root, text='WEAK', font=('calibre',10, 'bold')).grid(row=3, column=2)
       
passw_label = tk.Label(root, text = 'Enter Your password: ', font = ('calibre',10,'bold')) 
   
passw_entry=tk.Entry(root, textvariable = passw_var, font = ('calibre',10,'normal'), show = '*') 
   
sub_btn=tk.Button(root,text = 'Submit', 
                  command = submit) 
   
passw_label.grid(row=1,column=0) 
passw_entry.grid(row=1,column=1) 
sub_btn.grid(row=2,column=1) 
   
root.mainloop() 
我想知道我在哪里犯了错误。 谢谢

使用全局调用访问全局变量

def submit(): 
    global numcheck, charcheck
    ......
使用全局调用访问全局变量

def submit(): 
    global numcheck, charcheck
    ......
您应该将charcheck和numcheck移动到函数中:

def提交: charcheck=0 numcheck=0 您应该将charcheck和numcheck移动到函数中:

def提交: charcheck=0 numcheck=0 函数def submit不知道charcheck变量。你必须对你的函数说它是全局的:

def submit():
    global charcheck
    password=passw_var.get()    
    passw_var.set("")
    for i in range(len(password)):
因此,添加全局charcheck是可行的,您的函数def submit不知道charcheck变量。你必须对你的函数说它是全局的:

def submit():
    global charcheck
    password=passw_var.get()    
    passw_var.set("")
    for i in range(len(password)):
因此它可以工作,添加全局字符检查也可以这样做:

def submit(numbcheck,charcheck)



sub_btn=tk.Button(...,command = lambda:submit(numcheck,charcheck))
您也可以这样做:

def submit(numbcheck,charcheck)



sub_btn=tk.Button(...,command = lambda:submit(numcheck,charcheck))

您的函数正在缩进中的局部范围中查找变量名。如果您不理解,请查阅Python变量范围教程。你可以用两种方法解决这个问题;将变量作为参数传递或使用全局关键字,如下面的代码:

def submit(): 
    global charcheck, numcheck, passw_var

    special_chars = "!@#$%^&*()_+[]\;',./{}|:\"<>?`~"
    password=passw_var.get()    
    passw_var.set("")
    for i in range(len(password)):
        if(password[i]) in special_chars:
            charcheck += 1

        elif (ord(password[i])>=48 and ord(password[i])<=57):
            numcheck += 1 

    if (len(password)>=7 and charcheck>=2 and numcheck>=2):
        result_label = tk.Label(root, text='STRONG', font=('calibre',10, 'bold')).grid(row=3, column=2)

    else:
        result_label = tk.Label(root, text='WEAK', font=('calibre',10, 'bold')).grid(row=3, column=2)

我还编辑了条件以检查特殊字符,这说明了所有字符,并且可读性更强。

您的函数正在缩进中的局部范围内查找变量名。如果您不理解,请查阅Python变量范围教程。你可以用两种方法解决这个问题;将变量作为参数传递或使用全局关键字,如下面的代码:

def submit(): 
    global charcheck, numcheck, passw_var

    special_chars = "!@#$%^&*()_+[]\;',./{}|:\"<>?`~"
    password=passw_var.get()    
    passw_var.set("")
    for i in range(len(password)):
        if(password[i]) in special_chars:
            charcheck += 1

        elif (ord(password[i])>=48 and ord(password[i])<=57):
            numcheck += 1 

    if (len(password)>=7 and charcheck>=2 and numcheck>=2):
        result_label = tk.Label(root, text='STRONG', font=('calibre',10, 'bold')).grid(row=3, column=2)

    else:
        result_label = tk.Label(root, text='WEAK', font=('calibre',10, 'bold')).grid(row=3, column=2)

我还编辑了条件以检查特殊字符,这说明了所有字符,并且更具可读性。

作为程序员,您应该非常、非常、非常努力地避免使用全局字符。@quamrana除了在此处使用全局字符外,还有什么其他方法?也许是作为参数传递?@quamrana是的,全局变量的使用不应该用作提示,您的结果标签为None,这意味着它不能在其他地方编辑。只是发现您也可以按照Rice在回答中所说的做,将您的按钮命令转换为sub_btn=tk.Button…,command=lambda:submitnumcheck,charcheck。通过这种方式,您可以摆脱全局和重置值。作为一名程序员,您应该非常、非常、非常努力地避免使用全局值。@quamrana除了在此处使用全局值之外,还有什么其他方法?也许是作为参数传递?@quamrana是的,全局变量的使用不应该用作提示,您的结果标签为None,这意味着它不能在其他地方编辑。只是发现您也可以按照Rice在回答中所说的做,将您的按钮命令转换为sub_btn=tk.Button…,command=lambda:submitnumcheck,charcheck。这样你就可以摆脱全局和重置价值我不相信全局的东西。这些变量不能在函数外访问。我不相信全局性的东西。这些变量不能在函数外部访问。啊,是的!现在可以了,谢谢你:过几天我就接受你的回答minutes@Liana但是请记住,每当函数运行时,charcheck和numcheck的值都将重置为0,记住这一点,这可能会有帮助,是的!现在可以了,谢谢你:过几天我就接受你的回答minutes@Liana但请记住,当函数运行时,charcheck和numcheck的值将重置为0,记住这一点,否则可能会有所帮助。如果您也可以更改此值以显示按钮应如何接受函数,则更好,因为这里定义了参数。@酷云可以,但您已经在注释中告诉了如何做,我将编辑并放置它。如果您也可以更改它,以显示按钮应该如何接受函数,那就更好了,因为这里定义了参数。@cool cloud好的,但您已经在注释中告诉了如何做,我将编辑并放置它。