【发布时间】:2015-10-04 17:14:46
【问题描述】:
作为一个 Python 的菜鸟,我最近几天一直在尝试使用语法来尝试创建某种想到的程序。我最初的想法是创建一个地方来创建您自己的密码并让代码检查它是否正常(1 个符号、大写、数字),如果是,它将被接受。所以我扩大了一点。
我的问题是我已经提出了一个复选框,我拼命地尝试得到它的状态,以便在检查时,将显示密码,如果不是,则是通过将被解密为“*”。
from tkinter import *
root = Tk()
root.resizable(width=FALSE, height=FALSE)
#
# Function checks if password meets the criteria: No space, 1 symbol, 1 number, 1 capital letter.
# Makes changes once criteria is met.
def checkPass(event):
global b
b = entryPass.get()
zDigit = sum(map(b.count, ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")))
zSymbol = sum(map(b.count, ("!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+", "[", "{", "]", "}", ";", ":", "'", ",", ">", ",", "<", "/", "|")))
zCapital = sum(map(b.count, ("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")))
zSpace = b.count(" ")
if zCapital > 0:
if zSymbol > 0:
if zDigit > 0:
if zSpace > 0:
print("You cannot include a space in your password!")
else:
print("Your password is suitable and has been created!")
buttonCreate.grid_forget()
entryPass.grid_forget()
c = len(b)
global d
d = "*" * c
global firmLabel
firmLabel = Label(root, text=d)
firmLabel.grid(row=2, column=1)
baseLabel['text'] = 'SUCCESS'
baseLabel.grid(columnspan=1)
password.grid(row=2, sticky=E)
firmLabel['text'] = d
showPassBtn.grid(row=3, columnspan=2)
else:
print("You need at least one number in your password!")
else:
print("You need at least one symbol in your password!")
else:
print("You need at least one capital letter in your password!")
#
# Function that is called when the checkbox is clicked to show/hide password.
def showPass(btnState):
if btnState == TRUE:
firmLabel['text'] = d
else:
firmLabel['text'] = b
#
# The labels/buttons/inputs are set onto the main GUI (root).
# Checkbutton
btnState = IntVar()
showPassBtn = Checkbutton(root, text="Show password", variable=btnState)
showPassBtn.bind('<Button-1>', showPass)
# Rest of GUI Elements
baseLabel = Label(root, text="Type a password")
baseLabel.grid(row=0, columnspan=2)
password = Label(root, text="Password: ")
entryPass = Entry(root)
entryPass.grid(row=2, column=1)
buttonCreate = Button(root, text="Create")
buttonCreate.bind('<Button-1>', checkPass)
buttonCreate.grid(columnspan=2)
root.mainloop()
我尝试了很多方法,通过不同的资源,其中大部分只是给我错误,我无法完成任务。我尝试以几种方式获取按钮的状态,但我现在已经失去了追踪(一种是我试图获取 btnState 的状态,但它告诉我该事件没有属性“get”或类似的东西),所以我真的很难找到解决方案。
对不起,如果代码混乱,正如我所说,我是一个完整的新手,还没有研究所有的语法,以便我可以优化我的程序。我知道有一些东西需要优化,比如字符检查系统,但现在它可以正常工作。
附:我设法通过使用以下方法使其工作:
def showPass(btnState):
if state == 0:
firmLabel['text'] = d
global state
state = 1
else:
firmLabel['text'] = b
state = 0
state = 1
但这只是避免了获取按钮状态并使用它来做一件事或另一件事的概念。
提前谢谢你!
【问题讨论】:
标签: python checkbox tkinter get state