如何检查密码是否正确

时间:2019-03-24 08:02:26

标签: python tkinter

我创建了一个函数,该函数检查是否没有密码或密码是否太短,但如果不输入密码,它始终会转到第一个if条件。 有人可以帮忙吗?

from tkinter import *
import random
import time
from tkinter import END

def __init__(self, button, master, event):
    pass

    a=["red","blue","green","cyan","lime","light 
    blue","orange","yellow","gold","silver","purple","pink"]
def change_color():
    tk.configure(background=random.choice(a))
    tk.update()

\这是函数

def checking(event):
    if bool(g)==False and bool(g)!=True:
        L3 = Label(tk, bg="blue", fg="red", text="Input a Password", font= 
        ("Arial", 11))
        L3.pack(anchor=NW)
        PassEnter.delete(0, END)
    elif len(g)>0 and len(g)<8:
        L2 = Label(tk, bg="blue", fg="red", text="The password must be 8 
        or longer", font=("Arial", 11))
        L2.pack(anchor=NW)
        PassEnter.delete(0, END)
    else:
        pass

tk = Tk()
tk.title("Cosa")
tk.geometry("500x500")
tk.wm_attributes("-topmost", 1)
tk.configure(background=random.choice(a))

L1 = Label(tk, fg="black", text="Password:", font=("Times new roman", 15))
L1.pack(anchor=NW)

\这是要检查的文本框

PassEnter = Entry(tk, bg="white", fg="black", bd=5)
PassEnter.pack(side="top", anchor=NW)
g=PassEnter.get()    
PassEnter.bind("<KeyPress-Return>", checking)

btn = Button(tk, bg="black", fg="white", text="Change background color", 
command=change_color)
btn.pack(side="bottom", anchor=SE)

while True:
    tk.update()
    time.sleep(0.01)

我希望密码的len小于8时,它告诉我密码长度必须大于8。如果密码为空,则应该告诉我输入密码。 但是它总是告诉我“输入密码”,即使它长于或短于8

0 个答案:

没有答案