Home > Blockchain >  How do I exclude specific characters from a password generator based on what the user inputs into an
How do I exclude specific characters from a password generator based on what the user inputs into an

Time:11-22

I am trying to create a password generator with a very very simple GUI. I want to provide the EU with the ability to exclude characters if they need to. Any help at all would be greatly appreciated.

Here is what I have thus far that is throwing "TypeError: can only concatenate str (not "int") to str" and I can't for the life of me process why...

import os, random, string
from tkinter import *


# Password generation based on ascii letters, digits, and punctuation. 
def gen_password(password_length):
    initial_chars = string.ascii_letters   string.digits   string.punctuation
    characters = []
    for char in initial_chars:
        if char not in excluded_chars:
            characters  = char
    else:
        print('Excluded characters: '   char)
    
    # Seed the password with a random seed
    random.seed = (os.urandom(1024))
    random_str = ''.join(random.choice(characters) for _ in range(password_length))
    return random_str

# This will update the length of the password we generate based on whatever input number is entered
def update_password():
    try:
        count_chars = int(pass_len.get())
    except ValueError:
        return
    
    pass_box.config(state=NORMAL)
    pass_box.delete(0, 'end')
    pass_box.insert(0, gen_password(count_chars))
    pass_box.config(state=NORMAL)
    
mainWindow = Tk()
mainWindow.title('Griffin Password Generator 2.0')
mainWindow.resizable(0,0)

frame = Frame(mainWindow)

frame.pack(side=TOP, pady=10, padx=10, fill=X, expand=1)
Label(frame, text="Password Length: ", anchor=E).grid(row=0, column=0, sticky=E)
default_value = 16
pass_len = Entry(frame)
pass_len.insert(0, default_value)
pass_len.grid(row=0, column=1)

Label(frame, text="Excluded Characters: ", anchor=E).grid(row=1, column=0, sticky=E)
default_exclusions = ''
excluded_chars = Entry(frame)
excluded_chars.insert(0, default_exclusions)
excluded_chars.grid(row=1, column=1)

btn = Button(frame, text="Generate Password")
btn['command'] = lambda: update_password()
btn.grid(row=0, column=2, rowspan=2, padx=10, ipadx=10)

Label(frame, text="Generated Password: ", anchor=E).grid(row=2, column=0, sticky=E)
pass_box = Entry(frame)
pass_box.grid(row=2, column=1)

update_password()

# Open main window
mainWindow.mainloop()

CodePudding user response:

I finally figured out the error (with the help of some smart pals):

The line excluded_chars = Entry(frame) seems to be saving the entry as a memory value or something. So to accommodate, I changed lines 10 and 13 to excluded_chars.get() which solved the issue.

CodePudding user response:

The problem comes from the line:

if char not in excluded_chars:

At this time, excluded_chars is a Tkinter Entry widget. You probably want to refer to the characters/text that are shown in the widget. So change the above to:

if char not in excluded_chars.get():

and your code will run without throwing that exception.

  • Related