0

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!

2

1 Answer 1

1

Your main mistake is that the showPass function (binded to a mouse event) does not take the CheckButton value as parameter, but it takes the event itself. See here for details. You should replace it with:

    def showPass(event)  

Then you can consider btnState as a global variable (you can define it with btnState=IntVar() upper in your code), and so use it to retrieve the CheckButton value. If btnState.get() == 0, then it means that CheckButton is NOT checked.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a lot for the help! It works just the way I intended now! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.