有问题在Python中获得正确的Checkbutton状态

时间:2015-10-04 17:14:47

标签: python checkbox tkinter get state

作为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的状态,但是它告诉我事件没有属性&#34;得到&#34;或者某些东西那种,所以我真的很难找到解决方案。

很抱歉,如果代码很乱,正如我所说的那样,我是一个完整的新手,并且已经超越了所有语法,以便我可以优化我的程序。我知道有一些要优化的东西,比如字符检查系统,但是现在它才有效。

P.S。我设法使用以下方法使其工作:

def showPass(btnState):
    if state == 0:
        firmLabel['text'] = d
        global state
        state = 1
    else:
        firmLabel['text'] = b
        state = 0

state = 1

但这只是避免了获取按钮状态并使用它做一件事的概念。

提前谢谢!

1 个答案:

答案 0 :(得分:1)

你的主要错误是showPass函数(绑定到鼠标事件)不将CheckButton值作为参数,但它接受事件本身。有关详细信息,请参阅here。您应该将其替换为:

    def showPass(event)  

然后您可以将btnState视为全局变量(您可以在代码中使用btnState = IntVar()upper来定义它),因此使用它来检索CheckButton值。如果btnState.get()== 0,则表示未检查CheckButton。