|
| 1 | +import tkinter as tk |
| 2 | +import random |
| 3 | +import string |
| 4 | +import pyperclip |
| 5 | + |
| 6 | +# Define the main function for generating the password |
| 7 | +def generate_password(): |
| 8 | + password_length = length_entry.get() |
| 9 | + if password_length.isdigit() and int(password_length) > 0: |
| 10 | + password = ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation, k=int(password_length))) |
| 11 | + password_entry.delete(0, tk.END) |
| 12 | + password_entry.insert(0, password) |
| 13 | + else: |
| 14 | + password_entry.delete(0, tk.END) |
| 15 | + password_entry.insert(0, "Invalid password length") |
| 16 | + |
| 17 | +# Define the function for copying the password to the clipboard |
| 18 | +def copy_password(): |
| 19 | + password = password_entry.get() |
| 20 | + pyperclip.copy(password) |
| 21 | + |
| 22 | +# Create the main window |
| 23 | +root = tk.Tk() |
| 24 | +root.title("Password Generator") |
| 25 | + |
| 26 | +# Create the password length label and entry widget |
| 27 | +length_label = tk.Label(root, text="Password Length:") |
| 28 | +length_label.pack(pady=10) |
| 29 | +length_entry = tk.Entry(root, width=5) |
| 30 | +length_entry.pack() |
| 31 | + |
| 32 | +# Create the "Generate Password" button |
| 33 | +generate_button = tk.Button(root, text="Generate Password", command=generate_password) |
| 34 | +generate_button.pack(pady=10) |
| 35 | + |
| 36 | +# Create the password entry widget |
| 37 | +password_label = tk.Label(root, text="Generated Password:") |
| 38 | +password_label.pack() |
| 39 | +password_entry = tk.Entry(root, width=30) |
| 40 | +password_entry.pack(pady=10) |
| 41 | + |
| 42 | +# Create the "Copy Password" button |
| 43 | +copy_button = tk.Button(root, text="Copy Password", command=copy_password) |
| 44 | +copy_button.pack(pady=10) |
| 45 | + |
| 46 | +# Set the focus on the length entry widget |
| 47 | +length_entry.focus() |
| 48 | + |
| 49 | +# Run the main loop |
| 50 | +root.mainloop() |
0 commit comments