Dominic Sham
Dominic Sham

Reputation: 307

The labels are not correct when I click the corresponding values

from tkinter import *
import random

def check():
    en.delete(0,END)
    letters = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
    signs = '@#$%^&*!<>'
    digits = '1234567890'
    if pw.get() == random.choice(digits) + random.choice(letters) + random.choice(signs):
        strong = 'Very good password'
        strength.set(strong)
    elif pw.get() == random.choice(digits) + random.choice(letters):
        med = 'Medium password strength'
        strength.set(med)
    elif pw.get() == random.choice(digits):
        weak = 'Password too weak! Use another one'
        strength.set(weak)
    elif len(pw.get()) < 8:
        tooweak = 'Password not acceptable!'
        strength.set(tooweak)
    


win = Tk()
win.title('Password Generator')

lb = Label(win,text='Create a password')
lb.pack(pady=10)

pw = StringVar()
en = Entry(win,textvariable=pw)
en.pack(pady=10)

btn = Button(win,text='Create',command=check)
btn.pack(pady=10)

strength = StringVar()
lbstr = Label(win,textvariable=strength)
lbstr.pack(pady=10)

There is no error in this program. However when I run, and using the if statements inside def check():, the labels can't be shown if I use passwords satisfied with conditions in strength.set(strong),strength.set(med) and strength.set(weak). May I ask is it possible to use set method to change the text of label? Also may I ask is it correct that I put random.choice(digits) + random.choice(letters) + random.choice(signs) if I want a password to have digits,letters and signs within the entry? For this question, I already solved the problem by doing researches. If I encounter the same problem again, I will organize well my script and check the spellings and whether the methods used are correct. Before asking new ones, I will try my best first and ask only necessary.

Upvotes: 0

Views: 47

Answers (1)

acw1668
acw1668

Reputation: 46687

First you called en.delete(0,END) at the beginning of check() function and it leads to pw.get() return empty string. It should be called at the end of check() function.

To check the weakness of the input password, you can create a list to hold the number of letters, signs and digits in the password. Then you can use the list to determine the weakness of the password:

def check():
    letters = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
    signs = '@#$%^&*!<>'
    digits = '1234567890'

    passwd = pw.get().strip()
    if len(passwd) < 8:
        tooweak = 'Password not acceptable!'
        strength.set(tooweak)
        lbstr.config(fg='red')
    else:        
        counts = [0, 0, 0]
        # go through each character in the password
        # and count the number of letters, signs and digits
        for c in passwd:
            if c in letters:
                counts[0] += 1
            elif c in signs:
                counts[1] += 1
            elif c in digits:
                counts[2] += 1
        print(counts)
        # check how many zeros are in the counts list
        zeros = counts.count(0)
        if zeros == 0:
            strong = 'Very good password'
            strength.set(strong)
            lbstr.config(fg='blue')
        elif zeros == 1:
            med = 'Medium password strength'
            strength.set(med)
            lbstr.config(fg='green')
        elif zeros == 2:
            weak = 'Password too weak! Use another one'
            strength.set(weak)
            lbstr.config(fg='red')
    # clear the password entry
    #en.delete(0,END)
    pw.set('')

Upvotes: 3

Related Questions