This code creates a **GUI-based random password generator application** using...

August 8, 2025 at 07:41 PM

import tkinter as tk from tkinter import messagebox import random # Character set dictionary char_sets = { "letters": 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', "letters_numbers": 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', "letters_special": 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-_=+;:<,>.?', "all": 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+;:<,>.?' } # Function to generate passwords def generate_passwords(num, length, use_numbers, use_special): passwords = [] if not use_numbers and not use_special: chars = char_sets["letters"] elif use_numbers and not use_special: chars = char_sets["letters_numbers"] elif not use_numbers and use_special: chars = char_sets["letters_special"] else: chars = char_sets["all"] for _ in range(num): password = ''.join(random.choice(chars) for _ in range(length)) passwords.append(password) return passwords # Event handler for Generate button def on_generate(): try: num = int(entry_num.get()) length = int(entry_length.get()) use_numbers = var_numbers.get() use_special = var_special.get() if num <= 0 or length <= 0: messagebox.showerror("Invalid Input", "Number of passwords and length must be positive integers.") return results = generate_passwords(num, length, use_numbers, use_special) text_output.delete(1.0, tk.END) for pwd in results: text_output.insert(tk.END, pwd + '\n') except ValueError: messagebox.showerror("Input Error", "Please enter valid numbers.") # Setting up the GUI root = tk.Tk() root.title("Random Password Generator") # Labels and Entry Fields tk.Label(root, text="How many passwords?").grid(row=0, column=0, padx=10, pady=5, sticky="e") entry_num = tk.Entry(root) entry_num.grid(row=0, column=1, padx=10, pady=5) tk.Label(root, text="Length of each password?").grid(row=1, column=0, padx=10, pady=5, sticky="e") entry_length = tk.Entry(root) entry_length.grid(row=1, column=1, padx=10, pady=5) # Checkbuttons for options var_numbers = tk.BooleanVar() var_special = tk.BooleanVar() chk_numbers = tk.Checkbutton(root, text="Include Numbers", variable=var_numbers) chk_numbers.grid(row=2, column=0, columnspan=2, sticky="w", padx=10) chk_special = tk.Checkbutton(root, text="Include Special Characters", variable=var_special) chk_special.grid(row=3, column=0, columnspan=2, sticky="w", padx=10) # Button to trigger password generation btn_generate = tk.Button(root, text="Generate Passwords", command=on_generate) btn_generate.grid(row=4, column=0, columnspan=2, pady=10) # Text widget for output text_output = tk.Text(root, height=10, width=50) text_output.grid(row=5, column=0, columnspan=2, padx=10, pady=10) # Start the application root.mainloop()

This code creates a GUI-based random password generator application using the tkinter library. Here's what it does:

Functionality:

  1. User Inputs:

    • Allows the user to specify:
      • How many passwords they want to generate.
      • The length of each password.
      • Whether passwords should include numbers.
      • Whether passwords should include special characters.
  2. Password Generation:

    • Based on the user inputs, it generates random passwords using the random.choice() function applied to custom character sets.
    • Character sets include alphabets, numbers, special characters, or combinations of these.
  3. Output:

    • Displays the generated passwords in a text box on the GUI.
    • If invalid input (e.g., non-integer values or non-positive numbers) is provided, it displays an error message using messagebox.showerror().

Key Parts of the Code:

  1. Character Set Dictionary:

    • Defines different combinations of characters (letters, letters_numbers, letters_special, all) used to generate custom passwords based on user requirements.
  2. Password Generation Logic:

    • The generate_passwords() function constructs passwords by randomly selecting characters (random.choice()) from the appropriate character set.
    • It accounts for the user's preferences (whether to include numbers or special characters).
  3. Event Handling:

    • The on_generate() function is triggered by clicking the "Generate Passwords" button. It:
      • Reads input values from the GUI fields.
      • Validates and processes these inputs.
      • Calls the generate_passwords() function with appropriate parameters.
      • Displays the generated passwords in the text_output widget.
  4. GUI Design:

    • The interface includes:
      • Entry fields: To take numeric inputs for the number and length of passwords.
      • Checkbuttons: For options to include numbers and special characters.
      • A Button: To trigger the generation of passwords.
      • A Text widget: To display the generated passwords.
  5. Error Handling:

    • Raises error messages if:
      • Non-integer or invalid values are provided.
      • Negative or zero values are entered for the number or length of passwords.

Example Usage:

  • Enter "5" in the first field (number of passwords), "10" in the second field (length of each password).
  • Check the options for "Include Numbers" and/or "Include Special Characters" based on your requirements.
  • Click "Generate Passwords" to see the resulting passwords displayed in the output text box.

Output:

  • For example, if you specify:
    • 5 passwords, each of length 10.
    • Include both numbers and special characters.
  • The generator might produce passwords like:
    d7@wEr!IxP
    Y$N2Lg!xPi
    jkF45*Et#z
    Pq_9bEKx@C
    Qz%Ty#7_VW
    

This piece of code essentially creates an interactive, user-friendly password generator tool.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node