-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpasswordgenerator_codesoft.py
More file actions
51 lines (41 loc) · 1.63 KB
/
passwordgenerator_codesoft.py
File metadata and controls
51 lines (41 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import tkinter as tk
import random
class PasswordGenerator:
def __init__(self, root):
self.root = root
self.root.title("Password Generator")
self.label = tk.Label(root, text="Password Generator")
self.label.grid(row=0, column=0, columnspan=2)
self.entry = tk.Entry(root, width=30)
self.entry.grid(row=1, column=0, columnspan=2)
self.pass_length_label = tk.Label(root, text="Password Length:")
self.pass_length_label.grid(row=2, column=0)
self.len = tk.Entry(root, width=20)
self.len.grid(row=2, column=1)
self.gen_button = tk.Button(root, text="Generate password", command=self.generate_password)
self.gen_button.grid(row=3, column=0, columnspan=2)
def generate_password(self):
try:
length = int(self.len.get())
if length <= 0:
self.entry.delete(0, tk.END)
self.entry.insert(0, "Invalid length")
else:
password = self.generated_password(length)
self.entry.delete(0, tk.END)
self.entry.insert(0, password)
except ValueError:
self.entry.delete(0, tk.END)
self.entry.insert(0, "Invalid Length")
def generated_password(self, length):
a = "abcdefghijklmnopqrstuvwxyz"
b = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
c = "123456789"
d = "!@#$%^&%&*><?:"
total = a + b + c + d
password = "".join(random.sample(total, length))
return password
if __name__ == "__main__":
root = tk.Tk()
password_generator = PasswordGenerator(root)
root.mainloop()