calfred2808

#Python Generate Password

Jul 19th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. # importing the tkinter module
  2. from tkinter import *
  3.  
  4. # importing the pyperclip module to use it to copy our generated
  5. # password to clipboard
  6. import pyperclip
  7.  
  8. # random module will be used in generating the random password
  9. import random
  10.  
  11. # initializing the tkinter
  12. root = Tk()
  13.  
  14. # setting the width and height of the gui
  15. root.geometry("400x400")    # x is small case here
  16.  
  17. # declaring a variable of string type and this variable will be
  18. # used to store the password generated
  19. passstr = StringVar()
  20.  
  21. # declaring a variable of integer type which will be used to
  22. # store the length of the password entered by the user
  23. passlen = IntVar()
  24.  
  25. # setting the length of the password to zero initially
  26. passlen.set(0)
  27.  
  28.  
  29. # function to generate the password
  30. def generate():
  31.     # storing the keys in a list which will be used to generate
  32.     # the password
  33.     pass1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  34.             'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  35.             'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
  36.             'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  37.             'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  38.             'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8',
  39.             '9', '0', ' ', '!', '@', '#', '$', '%', '^', '&',
  40.             '*', '(', ')']
  41.  
  42.     # declaring the empty string
  43.     password = ""
  44.  
  45.     # loop to generate the random password of the length entered          
  46.     # by the user
  47.     for x in range(passlen.get()):
  48.         password = password + random.choice(pass1)
  49.  
  50.     # setting the password to the entry widget
  51.     passstr.set(password)
  52.  
  53. # function to copy the password to the clipboard
  54. def copytoclipboard():
  55.     random_password = passstr.get()
  56.     pyperclip.copy(random_password)
  57.  
  58. # Creating a text label widget
  59. Label(root, text="Password Generator Application", font="calibri 20 bold").pack()
  60.  
  61. # Creating a text label widget
  62. Label(root, text="Enter password length").pack(pady=3)
  63.  
  64. # Creating a entry widget to take password length entered by the
  65. # user
  66. Entry(root, textvariable=passlen).pack(pady=3)
  67.  
  68. # button to call the generate function
  69. Button(root, text="Generate Password", command=generate).pack(pady=7)
  70.  
  71. # entry widget to show the generated password
  72. Entry(root, textvariable=passstr).pack(pady=3)
  73.  
  74. # button to call the copytoclipboard function
  75. Button(root, text="Copy to clipboard", command=copytoclipboard).pack()
  76.  
  77. # mainloop() is an infinite loop used to run the application when
  78. # it's in ready state
  79. root.mainloop()
Add Comment
Please, Sign In to add comment