|
| 1 | +'''Python program for generating random password''' |
| 2 | + |
| 3 | +#Importing the modules |
| 4 | +import tkinter as tk |
| 5 | +from tkinter import ttk |
| 6 | + |
| 7 | +import random |
| 8 | +import string |
| 9 | + |
| 10 | +#Data setting |
| 11 | +lowercase = list(string.ascii_lowercase) |
| 12 | +uppercase = list(string.ascii_uppercase) |
| 13 | +digits = list(string.digits) |
| 14 | +symbols = list(string.punctuation) |
| 15 | + |
| 16 | + |
| 17 | +class Password: |
| 18 | + |
| 19 | + """Class for generating random strong passwords |
| 20 | + Attributes: |
| 21 | + length (int): Length of the password |
| 22 | + pwd (str): The password |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__(self, char, length): |
| 26 | + self.char = char |
| 27 | + self.length = length |
| 28 | + self.charset = [] |
| 29 | + self.pwd = None |
| 30 | + |
| 31 | + def setchar(self): |
| 32 | + """Setting character set.""" |
| 33 | + |
| 34 | + if 'l' in self.char: self.charset.extend(lowercase) |
| 35 | + if 'u' in self.char: self.charset.extend(uppercase) |
| 36 | + if 'd' in self.char: self.charset.extend(digits) |
| 37 | + if 's' in self.char: self.charset.extend(symbols) |
| 38 | + |
| 39 | + def password_gen(self): |
| 40 | + """Return the password |
| 41 | +
|
| 42 | + Returns: |
| 43 | + str: The password |
| 44 | + """ |
| 45 | + if len(self.char) == 0: |
| 46 | + self.charset.extend(lowercase) |
| 47 | + |
| 48 | + if len(self.length) == 0: |
| 49 | + self.length = 10 # By default, length is 10 |
| 50 | + else: |
| 51 | + self.length = int(self.length) |
| 52 | + |
| 53 | + pwdlist = random.choices(self.charset, k=self.length) |
| 54 | + self.pwd = ''.join(pwdlist) |
| 55 | + return self.pwd |
| 56 | + |
| 57 | + |
| 58 | +wind = tk.Tk() |
| 59 | + |
| 60 | +def generate(): |
| 61 | + global ch |
| 62 | + |
| 63 | + if u.get(): ch += 'u' |
| 64 | + if l.get(): ch += 'l' |
| 65 | + if d.get(): ch += 'd' |
| 66 | + if s.get(): ch += 's' |
| 67 | + |
| 68 | + password = Password(ch, len_entry.get()) |
| 69 | + password.setchar() |
| 70 | + |
| 71 | + pwd.set(password.password_gen()) |
| 72 | + ch = '' |
| 73 | + |
| 74 | +# Initialise int variables |
| 75 | +ch = '' |
| 76 | +u = tk.IntVar() |
| 77 | +d = tk.IntVar() |
| 78 | +s = tk.IntVar() |
| 79 | +l = tk.IntVar() |
| 80 | +pwd = tk.StringVar() |
| 81 | + |
| 82 | +# Main program |
| 83 | +wind.title('Paasword Generator') |
| 84 | +wind.geometry('400x300') |
| 85 | +wind.resizable(0, 0) |
| 86 | + |
| 87 | +head_label = ttk.Label(wind, text='Password Generator', |
| 88 | + font=('Bodoni MT', 30, 'bold')) |
| 89 | +head_label.grid(row=0, column=0, columnspan=2, pady=5) |
| 90 | + |
| 91 | + |
| 92 | +len_label = ttk.Label(wind, text='Enter length', font=('Arial', 10)) |
| 93 | +len_label.grid(row=1, column=0, pady=10) |
| 94 | + |
| 95 | +len_entry = ttk.Entry(wind, font=('Arial', 10, 'bold')) |
| 96 | +len_entry.grid(row=1, column=1, pady=10) |
| 97 | + |
| 98 | +upper_box = ttk.Checkbutton(wind, text='Uppercase', variable=u) |
| 99 | +upper_box.grid(row=2, column=0) |
| 100 | + |
| 101 | +lower_box = ttk.Checkbutton(wind, text='Lowercase', variable=l) |
| 102 | +lower_box.grid(row=2, column=1) |
| 103 | + |
| 104 | +digit_box = ttk.Checkbutton(wind, text='Digits', variable=d) |
| 105 | +digit_box.grid(row=3, column=0) |
| 106 | + |
| 107 | +symbol_box = ttk.Checkbutton(wind, text='Symbols', variable=s) |
| 108 | +symbol_box.grid(row=3, column=1) |
| 109 | + |
| 110 | +generate_button = ttk.Button(wind, text='Generate', command=generate) |
| 111 | +generate_button.grid(row=4, column=0, columnspan=2, pady=10) |
| 112 | + |
| 113 | +t_label = ttk.Label(wind, text=' Password :', font=('Arial', 10)) |
| 114 | +t_label.grid(row=5, column=0, pady=10) |
| 115 | + |
| 116 | +password_disp = ttk.Entry(wind, font=('Arial', 10, 'bold'), |
| 117 | + textvariable=pwd, width=30) |
| 118 | +password_disp.grid(row=5, column=1, pady=10) |
| 119 | + |
| 120 | +wind.mainloop() |
0 commit comments