我已经分配了一个功课,将tkinter GUI添加到普通的python代码中。
这是我的代码:
from tkinter import *
Window = Tk()
PasswordActual = Entry(Window)
Password = str(PasswordActual)
L1 = Label(Window, text = "Enter password:")
L2 = Label(Window)
def Main():
PasswordCheck1 = len(Password)
NumbersList = []
CapsList = []
LowersList = []
def PasswordProcesser(WhatCharacters, CharactersInPassword):
for Characters in WhatCharacters:
if Characters in Password:
PasswordCheck2 = Characters in Password
if CharactersInPassword == "Numbers":
NumbersList.append(PasswordCheck2)
elif CharactersInPassword == "Caps":
CapsList.append(PasswordCheck2)
elif CharactersInPassword == "Lowers":
LowersList.append(PasswordCheck2)
if PasswordCheck1 >= 6 and PasswordCheck1 <= 12:
PasswordProcesser(["1","2","3","4","5","6","7","8","9"], "Numbers")
PasswordProcesser(["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","Q","Y","Z"], "Caps")
PasswordProcesser(["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"], "Lowers")
NumbersInPassword = sum(NumbersList)
CapsInPassword = sum(CapsList)
LowersInPassword = sum(LowersList)
if NumbersInPassword == 0 and CapsInPassword == 0 and LowersInPassword > 0 or CapsInPassword == 0 and NumbersInPassword > 0 and LowersInPassword == 0 or LowersInPassword == 0 and CapsInPassword > 0 and NumbersInPassword == 0:
L2.configure(text = "Password is weak")
elif NumbersInPassword == 0 and CapsInPassword > 0 and LowersInPassword > 0 or CapsInPassword == 0 and LowersInPassword > 0 and NumbersInPassword > 0 or LowersInPassword == 0 and NumbersInPassword > 0 and CapsInPassword > 0:
L2.configure(text = "Password is medium")
else:
L2.configure(text = "Password is strong")
else:
L2.configure(text = "Password too long or short")
Checker = Button(Window, text = "Check Password", command = Main)
L1.grid(row = 1, column = 1)
PasswordActual.grid(row = 1, column = 2)
Checker.grid(row = 2, column = 1)
L2.grid(row = 2, column = 2)
我想要的输出告诉用户他或她的代码是弱,中等或强。它没有添加tkinter的东西,但它反复告诉我,我的密码很弱。这有什么理由,还是我只是愚蠢? (我是tkinter初学者)Thx提前。
这是我没有GUI的(工作)代码:
def Main2():
Password = input("What is your password?: ")
PasswordCheck1 = len(Password)
NumbersList = []
CapsList = []
LowersList = []
def ExitFunc():
Exit = input("Do you want to exit or retry?: ")
if Exit == "exit" or Exit == "Exit":
exit
elif Exit == "retry" or Exit == "Retry":
Main2()
else:
print("Invalid input, enter exit or retry")
ExitFunc()
def PasswordProcesser(WhatCharacters, CharactersInPassword):
for Characters in WhatCharacters:
if Characters in Password:
PasswordCheck2 = Characters in Password
if CharactersInPassword == "Numbers":
NumbersList.append(PasswordCheck2)
elif CharactersInPassword == "Caps":
CapsList.append(PasswordCheck2)
elif CharactersInPassword == "Lowers":
LowersList.append(PasswordCheck2)
if PasswordCheck1 >= 6 and PasswordCheck1 <= 12:
PasswordProcesser(["1","2","3","4","5","6","7","8","9"], "Numbers")
PasswordProcesser(["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","Q","Y","Z"], "Caps")
PasswordProcesser(["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"], "Lowers")
NumbersInPassword = sum(NumbersList)
CapsInPassword = sum(CapsList)
LowersInPassword = sum(LowersList)
if NumbersInPassword == 0 and CapsInPassword == 0 and LowersInPassword > 0 or CapsInPassword == 0 and NumbersInPassword > 0 and LowersInPassword == 0 or LowersInPassword == 0 and CapsInPassword > 0 and NumbersInPassword == 0:
print("Password is weak, try and include capital, lowercase and numerical characters")
ExitFunc()
elif NumbersInPassword == 0 and CapsInPassword > 0 and LowersInPassword > 0 or CapsInPassword == 0 and LowersInPassword > 0 and NumbersInPassword > 0 or LowersInPassword == 0 and NumbersInPassword > 0 and CapsInPassword > 0:
print("Password is medium, try and include capital, lowercase and numerical characters")
ExitFunc()
else:
print("Password is strong")
ExitFunc()
else:
print("Password too long or short, it needs to be no shorter than 6 and no longer than 12")
ExitFunc()
Main2()
答案 0 :(得分:2)
问题在于密码获取。
您应该在Entry
函数中Main
获取密码,如下所示:
PasswordCheck1 = len(PasswordActual.get())
有必要删除第三行Password = str(PasswordActual)
,因为它是对PasswordActual
的引用。
看看这个:
from tkinter import *
Window = Tk()
PasswordActual = Entry(Window)
L1 = Label(Window, text = "Enter password:")
L2 = Label(Window)
def Main():
Password = PasswordActual.get()
PasswordCheck1 = len(Password)
..............................