dimanche 4 octobre 2015

Having issue getting proper Checkbutton state in Python

As a complete noob in Python I stared to play around last few days with the syntax to try and create some kind of a program that came to mind. My initial idea was to make a place to create your own password and have the code check if it is ok (1 symbol, capital, number) and if it is, it will get accepted. So I expanded a bit.

My issue is that I have put a checkbox and am trying desperately to get its state, so that whenever it is checked, the password will be shown, and if it isn't, the pass will be decrypted as '*'.

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()

I tried numerous ways, through different resources, most of which just gave me errors and I could not complete the task. I tried getting the state of the button in several ways of which I lost track by now (one was I tried to get the state of btnState, but it told me the event has no attribute "get" or something of that sort), so I really am struggling to find a solution.

Sorry if the code is messy, as I said, I'm a complete newbie and haven't gone over all the syntax so that I can optimize my program. I know there is stuff to optimize, like the character checking system, but for now it just works.

P.S. I managed to get it working by using the following:

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

state = 1

But that is just avoiding the concept of getting the button state and using it to do one thing or another.

Thank you in advance!




Aucun commentaire:

Enregistrer un commentaire