|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import * |
| 3 | +from tkinter import messagebox |
| 4 | +import random |
| 5 | +import string |
| 6 | +import pyperclip |
| 7 | + |
| 8 | +#main window |
| 9 | +root = tk.Tk() |
| 10 | +#title of the window |
| 11 | +root.title("Random Password Generator") |
| 12 | +#disabling resizing of window |
| 13 | +root.resizable(0, 0) |
| 14 | + |
| 15 | +#variables for Password |
| 16 | +upper = string.ascii_uppercase |
| 17 | +lower = string.ascii_lowercase |
| 18 | +num = string.digits |
| 19 | +punc = string.punctuation |
| 20 | + |
| 21 | +pass_str = StringVar() |
| 22 | +pass_wrd = StringVar() |
| 23 | + |
| 24 | +#function to create a random password |
| 25 | +def create_pass(): |
| 26 | + password.delete(0, END) |
| 27 | + Len = len.get() |
| 28 | + strn = opt.get() |
| 29 | + pass_wrd = "" |
| 30 | + #password of low strength |
| 31 | + if(strn==1): |
| 32 | + for i in range (0,Len): |
| 33 | + pass_wrd = pass_wrd + random.choice(lower + num) |
| 34 | + return pass_wrd |
| 35 | + #password of medium strength |
| 36 | + elif(strn==2): |
| 37 | + for i in range (0, Len): |
| 38 | + pass_wrd = pass_wrd + random.choice(lower + upper + num) |
| 39 | + return pass_wrd |
| 40 | + #password of high strength |
| 41 | + elif(strn==3): |
| 42 | + for i in range (0, Len): |
| 43 | + pass_wrd = pass_wrd + random.choice(lower + upper + num + punc) |
| 44 | + return pass_wrd |
| 45 | + else: |
| 46 | + messagebox.showwarning("Warning", "Select all parameters") |
| 47 | + |
| 48 | +#function to generate the password |
| 49 | +def gen(): |
| 50 | + pass_str = create_pass() |
| 51 | + password.insert(0, pass_str) |
| 52 | + |
| 53 | +#function to copy password to clipboard |
| 54 | +def cpy(): |
| 55 | + random_pass = password.get() |
| 56 | + pyperclip.copy(random_pass) |
| 57 | + messagebox.showinfo("Message", "Copied to clipboard !") |
| 58 | + |
| 59 | +#Adding frames |
| 60 | + |
| 61 | +#---frame for top name--- |
| 62 | +top = Frame(root, width = 700, height = 50, bd=8, relief="raise") |
| 63 | +top.pack(side = TOP) |
| 64 | + |
| 65 | +#---frame for length of password--- |
| 66 | +Length = Frame(root, width = 700, height = 50, bd=8, relief="raise") |
| 67 | +Length.pack(side = TOP) |
| 68 | + |
| 69 | +#---frame for strength of password--- |
| 70 | +strength = Frame(root, width = 300, height = 450, bd=8, relief="raise", padx=100, pady=20) |
| 71 | +strength.pack(side = LEFT) |
| 72 | + |
| 73 | +#---frame for output--- |
| 74 | +output = Frame(root, width = 450, height = 450, bd=8, relief="raise") |
| 75 | +output.pack(side = RIGHT) |
| 76 | + |
| 77 | + |
| 78 | +#Adding widgets |
| 79 | +greeting = Label(top, text="Random Password Generator", width=40, height=2, font = ("Lucida Console", 20, "italic")) |
| 80 | +greeting.grid(padx = 18) |
| 81 | + |
| 82 | +#---length of password--- |
| 83 | +lengthlabel = Label(Length, text="Length of Password", width=20, height=5, font = ("Arial", 10, "bold")) |
| 84 | +lengthlabel.grid(row = 5, column = 1, padx = 3, pady = 10) |
| 85 | + |
| 86 | +len = IntVar() |
| 87 | +scale = Scale(Length, orient = HORIZONTAL, from_ = 6, to = 24, tickinterval = 1, length = 500, variable=len) |
| 88 | +scale.grid(row = 5, column = 2) |
| 89 | + |
| 90 | + |
| 91 | +#---strength of password--- |
| 92 | +strengthlabel = Label(strength, text = "Strength",justify="center", width = 6, height = 2, font = ("Arial", 12, "bold")) |
| 93 | +strengthlabel.grid(row = 10, column = 2, pady = 10) |
| 94 | + |
| 95 | +opt = IntVar() |
| 96 | +c1 = Radiobutton(strength, text="Low", width = 6, height = 2 , variable=opt, value = 1, font = ("Arial", 12)) |
| 97 | +c1.grid(row = 12, column = 2, ipadx = 2, ipady = 2, sticky = 'E', pady = 5, padx = 5) |
| 98 | + |
| 99 | +c2 = Radiobutton(strength, text="Medium", width = 6, height = 2, variable=opt, value = 2, font = ("Arial", 12)) |
| 100 | +c2.grid(row = 14, column = 2, ipadx = 2, ipady = 2, sticky = 'E', pady = 5, padx = 5) |
| 101 | + |
| 102 | +c3 = Radiobutton(strength, text="High", width = 6, height = 2, variable=opt, value = 3, font = ("Arial", 12)) |
| 103 | +c3.grid(row = 16, column = 2, ipadx = 2, ipady = 2, sticky = 'E', pady = 5, padx = 5) |
| 104 | + |
| 105 | + |
| 106 | +#---output--- |
| 107 | +genbtn = Button(output, text = "Generate Password", justify = "center", width = 20, height = 3, command=gen, font = ("Arial", 12, "bold")) |
| 108 | +genbtn.grid(row = 1, column = 4, padx = 30, pady = 17) |
| 109 | + |
| 110 | +password = Entry(output, justify = "center", width=50, textvariable = pass_str) |
| 111 | +password.grid(row = 2, column = 4, padx = 30, pady = 20, ipady = 8) |
| 112 | + |
| 113 | +copybtn = Button(output, text = "Copy", justify = "center", width = 20, height = 3, font = ("Arial", 12, "bold"), command=cpy) |
| 114 | +copybtn.grid(row = 3, column = 4, padx = 30, pady = 17) |
| 115 | + |
| 116 | +root.mainloop() |
0 commit comments