在 Python 中获取正确的 Checkbutton 状态时遇到问题
Having issue getting proper Checkbutton state in Python
作为 Python 中的一个完全菜鸟,我在过去几天开始尝试使用语法来尝试创建某种想到的程序。我最初的想法是创建一个地方来创建您自己的密码,并让代码检查它是否正确(1 个符号、大写字母、数字),如果正确,它将被接受。所以我扩大了一点。
我的问题是我放了一个复选框并拼命尝试获取它的状态,这样每当它被选中时,就会显示密码,如果没有't,通行证会被解密为'*'。
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" 或类似的东西) , 所以我真的很难找到解决办法。
对不起,如果代码很乱,正如我所说,我是一个完全的新手,并没有复习所有的语法以便我可以优化我的程序。我知道有一些东西需要优化,比如字符检查系统,但现在它可以正常工作了。
P.S。我设法通过使用以下方法让它工作:
def showPass(btnState):
if state == 0:
firmLabel['text'] = d
global state
state = 1
else:
firmLabel['text'] = b
state = 0
state = 1
但这只是避免了获取按钮状态并使用它来做某件事的概念。
提前致谢!
您的主要错误是 showPass 函数(绑定到鼠标事件)没有将 CheckButton 值作为参数,而是将事件本身作为参数。有关详细信息,请参阅 here。您应该将其替换为:
def showPass(event)
然后您可以将 btnState 视为一个全局变量(您可以在代码的上方使用 btnState=IntVar() 定义它),然后使用它来检索 CheckButton 值。如果 btnState.get() == 0,则表示未选中 CheckButton。
作为 Python 中的一个完全菜鸟,我在过去几天开始尝试使用语法来尝试创建某种想到的程序。我最初的想法是创建一个地方来创建您自己的密码,并让代码检查它是否正确(1 个符号、大写字母、数字),如果正确,它将被接受。所以我扩大了一点。
我的问题是我放了一个复选框并拼命尝试获取它的状态,这样每当它被选中时,就会显示密码,如果没有't,通行证会被解密为'*'。
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" 或类似的东西) , 所以我真的很难找到解决办法。
对不起,如果代码很乱,正如我所说,我是一个完全的新手,并没有复习所有的语法以便我可以优化我的程序。我知道有一些东西需要优化,比如字符检查系统,但现在它可以正常工作了。
P.S。我设法通过使用以下方法让它工作:
def showPass(btnState):
if state == 0:
firmLabel['text'] = d
global state
state = 1
else:
firmLabel['text'] = b
state = 0
state = 1
但这只是避免了获取按钮状态并使用它来做某件事的概念。
提前致谢!
您的主要错误是 showPass 函数(绑定到鼠标事件)没有将 CheckButton 值作为参数,而是将事件本身作为参数。有关详细信息,请参阅 here。您应该将其替换为:
def showPass(event)
然后您可以将 btnState 视为一个全局变量(您可以在代码的上方使用 btnState=IntVar() 定义它),然后使用它来检索 CheckButton 值。如果 btnState.get() == 0,则表示未选中 CheckButton。