Programmin-in-Python

GUI Password Manager

Dec 21st, 2020 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.43 KB | None | 0 0
  1. # This requires MySQL to be installed in your system
  2. # The following modules must also be installed :-
  3. #   i) pymysql or mysql-connector
  4. # First Run the Tables_in_MySQL.py which will create the databases and tables in MySQL, which is available in the following GitHub Link : https://github.com/Programmin-in-Python/Password_Manager.git
  5.  
  6. import tkinter as tk
  7. from tkinter import messagebox
  8. from sys import platform
  9. from hashlib import sha512
  10. try : import pymysql as cntr
  11. except ImportError: import mysql.connector as cntr
  12.  
  13. if platform == "linux":
  14.     db = cntr.connect(host="localhost", user='JB', passwd='manager', database="pasman")
  15. else:
  16.     db = cntr.connect(host="localhost", user='root', passwd='manager', database="pasman")
  17.  
  18. cur = db.cursor()
  19.  
  20. def MainWindow(LocUser):
  21.     root = tk.Tk()
  22.     root.title('Password Manager')
  23.     #root.iconbitmap("icon.ico")
  24.     root.geometry(f'{root.winfo_screenwidth()}x{root.winfo_screenheight()}+0+0')
  25.     #root.resizable(False,False)
  26.  
  27.     #============================== Variables ===============================
  28.     LocUser = LocUser
  29.     Website = tk.StringVar(root)
  30.     Email = tk.StringVar(root)
  31.     Username = tk.StringVar(root)
  32.     Password = tk.StringVar(root)
  33.  
  34.     #============================== Functions =============================
  35.     def disp() :
  36.         cur.execute(f"select * from AccountDetails where user = '{LocUser}';")
  37.         L1 = cur.fetchall()
  38.         T1 = ()        
  39.         if any(L1) :
  40.             for i in L1 :
  41.                 T1 += (f"{i[1]}\t\t\t\t\t {i[2]}\t\t\t\t\t {i[3]}\t\t\t\t\t {i[4]}\t\t\t\t\t\n" + '-'.center(316 , '-') + '\n',)
  42.         return T1
  43.  
  44.     def ADD():
  45.         if Website.get() == '' :
  46.             txtWebsite.configure(bg = 'red' , fg = 'white')
  47.             messagebox.showerror("Add New Details" , "You've left the Website field EMPTY!!!  All fields are MUST.")
  48.         elif Email.get() == '' :
  49.             txtEmail.configure(bg = 'red' , fg = 'white')
  50.             messagebox.showerror("Add New Details" , "You've left the Email ID field EMPTY!!!  All fields are MUST.")
  51.         elif Username.get() == '' :
  52.             txtUsername.configure(bg = 'red' , fg = 'white')
  53.             messagebox.showerror("Add New Details" , "You've left the Username field EMPTY!!!  All fields are MUST.")
  54.         elif Password.get() == '' :
  55.             txtPassword.configure(bg = 'red' , fg = 'white')
  56.             messagebox.showerror("Add New Details" , "You've left the Password field EMPTY!!!  All fields are MUST.")
  57.         else:
  58.             cur.execute(f"insert into AccountDetails values('{LocUser}','{Website.get()}','{Email.get()}','{Username.get()}','{Password.get()}');")
  59.             db.commit()
  60.             messagebox.showinfo("Add New Details","Details have been SUCCESSFULLY Added!!!")            
  61.             txtDetails.insert('end' , f'{Website.get()}\t\t\t\t\t {Email.get()}\t\t\t\t\t {Username.get()}\t\t\t\t\t {Password.get()}\n')
  62.             txtDetails.insert('end' , ('-'.center(316 , '-') + '\n'))
  63.             Website.set('')
  64.             Email.set('')
  65.             Username.set('')
  66.             Password.set('')
  67.  
  68.     #============================== Frame Widgets ===========================
  69.     Mainframe = tk.Frame(root)
  70.     Mainframe.grid()
  71.  
  72.     Tops = tk.Frame(Mainframe , bd = 10 , relief = 'ridge')
  73.     Tops.pack(side = 'top')
  74.  
  75.     lblTitle = tk.Label(Tops , width = 30 , font = ('arial' , 40 , 'bold') ,
  76.                                 text = 'Password Manager' , justify = 'center')
  77.     lblTitle.grid(padx = 150)
  78.  
  79.     Membername = tk.LabelFrame(Mainframe , bd = 10 , width = 1300 , height = 300 ,
  80.                                 font = ('arial' , 12 , 'bold') , text = 'Add New Details' , relief = 'ridge')
  81.     Membername.pack(padx = 38 , side = 'top')
  82.  
  83.     DetailsFrame = tk.LabelFrame(Mainframe , bd = 10 , width = 2000 , height = 200 ,
  84.                                         font = ('arial' , 12 , 'bold') , text = 'Your Details' , relief = 'ridge')
  85.     DetailsFrame.pack(padx = 38 , side = 'top')
  86.  
  87.     #============================== Label and Entry Widgets ===========================
  88.     lblWebsite = tk.Label(Membername , font = ('arial' , 16 , 'bold') ,
  89.                                 text = 'Website URL' , bd = 7)
  90.     lblWebsite.grid(row = 0 , column = 0 , sticky = 'n')        
  91.     txtWebsite = tk.Entry(Membername , font = ('arial' , 13 , 'bold') ,
  92.                                 textvariable = Website , bd = 7 , insertwidth = 2)
  93.     txtWebsite.grid(row = 0 , column = 1)
  94.  
  95.     lblEmail = tk.Label(Membername , font = ('arial' , 16 , 'bold') ,
  96.                                 text = 'Email ID' , bd = 7)
  97.     lblEmail.grid(row = 1 , column = 0 , sticky = 'n')        
  98.     txtEmail = tk.Entry(Membername , font = ('arial' , 13 , 'bold') ,
  99.                                 textvariable = Email , bd = 7 , insertwidth = 2)
  100.     txtEmail.grid(row = 1 , column = 1)
  101.  
  102.     lblUsername = tk.Label(Membername , font = ('arial' , 16 , 'bold') ,
  103.                                 text = 'Username' , bd = 7)
  104.     lblUsername.grid(row = 0 , column = 3 , sticky = 'n')        
  105.     txtUsername = tk.Entry(Membername , font = ('arial' , 13 , 'bold') ,
  106.                                 textvariable = Username , bd = 7 , insertwidth = 2)
  107.     txtUsername.grid(row = 0 , column = 4)
  108.  
  109.     lblPassword = tk.Label(Membername , font = ('arial' , 16 , 'bold') ,
  110.                                 text = 'Password' , bd = 7)
  111.     lblPassword.grid(row = 1 , column = 3 , sticky = 'n')        
  112.     txtPassword = tk.Entry(Membername , font = ('arial' , 13 , 'bold') , show='*',
  113.                                 textvariable = Password , bd = 7 , insertwidth = 2)
  114.     txtPassword.grid(row = 1 , column = 4)
  115.  
  116.     btnPrint = tk.Button(Membername, bd = 5,
  117.                                 font = ('arial' , 16 , 'bold') , text = 'Add' , command = ADD)    
  118.     btnPrint.grid(row = 1 , column = 5)
  119.  
  120.     #============================== Text Widget============================
  121.     txtDetails = tk.Text(DetailsFrame , width = 181 , height = 20 , font = ('arial' , 10 , 'bold'))
  122.     txtDetails.grid(row = 0 , column = 0 , columnspan = 4)
  123.     txtDetails.insert('end' , ('-'.center(316 , '-') + '\n'))
  124.     txtDetails.insert('end' , '     Website URLs\t\t\t\t\t       Email ID\t\t\t\t           Username\t\t\t\t\t\tPassword\n')
  125.     txtDetails.insert('end' , ('-'.center(316 , '-') + '\n'))
  126.     for i in disp() :
  127.         txtDetails.insert('end' , i)
  128.  
  129.     root.mainloop()
  130.  
  131. def register(root) :
  132.     root.destroy()
  133.  
  134.     root_reg = tk.Tk()
  135.     root_reg.title("Register")
  136.     #root_reg.iconbitmap("icon.ico")
  137.     root_reg.group()
  138.  
  139.     name = tk.StringVar(root_reg)
  140.     Email = tk.StringVar(root_reg)
  141.     username = tk.StringVar(root_reg)
  142.     password = tk.StringVar(root_reg)
  143.     password2 = tk.StringVar(root_reg)
  144.  
  145.     #===================================== Widgets ===============================
  146.     lblName = tk.Label(root_reg , font = ('arial' , 16 , 'bold') ,
  147.                            text = 'Enter Full Name : ')
  148.     lblName.grid(row = 0 , column = 0)
  149.     txtName  = tk.Entry(root_reg , font = ('arial' , 13 , 'bold') , bd = 7,
  150.                             textvariable = name , insertwidth = 2)
  151.     txtName.grid(row = 0 , column = 2)
  152.  
  153.     lblEmail = tk.Label(root_reg , font = ('arial' , 16 , 'bold') ,
  154.                            text = 'Enter E-Mail ID : ')
  155.     lblEmail.grid(row = 2 , column = 0)
  156.     txtEmail = tk.Entry(root_reg , font = ('arial' , 13 , 'bold') , bd = 7,
  157.                             textvariable = Email , insertwidth = 2)
  158.     txtEmail.grid(row = 2 , column = 2)
  159.  
  160.     lblUsername = tk.Label(root_reg , font = ('arial' , 16 , 'bold') ,
  161.                            text = 'Enter Username : ')
  162.     lblUsername.grid(row = 4 , column = 0)
  163.     txtUsername  = tk.Entry(root_reg , font = ('arial' , 13 , 'bold') , bd = 7,
  164.                             textvariable = username , insertwidth = 2)
  165.     txtUsername.grid(row = 4 , column = 2)
  166.  
  167.     lblPassword = tk.Label(root_reg , font = ('arial' , 16 , 'bold') ,
  168.                            text = 'Enter Password : ')
  169.     lblPassword.grid(row = 6 , column = 0)
  170.     txtPassword  = tk.Entry(root_reg , font = ('arial' , 13 , 'bold') , bd = 7,
  171.                             textvariable = password , insertwidth = 2 , show = '*')
  172.     txtPassword.grid(row = 6 , column = 2)
  173.  
  174.     lblPassword2 = tk.Label(root_reg , font = ('arial' , 16 , 'bold') ,
  175.                            text = 'Confirm Password : ')
  176.     lblPassword2.grid(row = 8 , column = 0)
  177.     txtPassword2 = tk.Entry(root_reg , font = ('arial' , 13 , 'bold') , bd = 7,
  178.                             textvariable = password2 , insertwidth = 2 , show = '*')
  179.     txtPassword2.grid(row = 8 , column = 2)
  180.  
  181.     #==================================== Inserting Data into MySQL ==============
  182.  
  183.     def reg() :
  184.         if name.get() == '' :
  185.             txtName.configure(bg = 'red' , fg = 'white')
  186.             messagebox.showerror("Register" , "You've left the Name field EMPTY!!!  All fields are MUST.")
  187.         elif Email.get() == '' :
  188.             txtEmail.configure(bg = 'red' , fg = 'white')
  189.             messagebox.showerror("Register" , "You've left the Email ID field EMPTY!!!  All fields are MUST.")
  190.         elif '@' not in Email.get() :
  191.             txtEmail.configure(bg = 'red' , fg = 'white')
  192.             messagebox.showerror("Register" , "You've Entered an INVALID Email ID!!! Please Check it.")
  193.         elif username.get() == '' :
  194.             txtUsername.configure(bg = 'red' , fg = 'white')
  195.             messagebox.showerror("Register" , "You've left the Username field EMPTY!!!  All fields are MUST.")
  196.         elif password.get() == '' :
  197.             txtPassword.configure(bg = 'red' , fg = 'white')
  198.             messagebox.showerror("Register" , "You've left the Password field EMPTY!!!  All fields are MUST.")
  199.         elif password2.get() == '' :
  200.             txtPassword2.configure(bg = 'red' , fg = 'white')
  201.             messagebox.showerror("Register" , "You've left the Password Confirmation field EMPTY!!!  All fields are MUST.")
  202.         else :
  203.             if password.get() == password2.get() :                
  204.                 passphrase = sha512(f"{password.get()}".encode('utf-32')).hexdigest()
  205.                 cur.execute(f"insert into users values('{name.get()}' , '{Email.get()}' , '{username.get()}' , '{passphrase}');")
  206.                 db.commit()
  207.                 messagebox.showinfo("Register" , "You've been REGISTERED Successfully.")
  208.                 login(root_reg)                
  209.             else : messagebox.showerror("Register" , "You've Entered DIFFERENT Passwords!!")
  210.  
  211.     def Exit() :
  212.         iExit = messagebox.askyesno('Register' , 'Do you want to quit ?')
  213.         if iExit > 0 :
  214.             root_reg.destroy()
  215.             return
  216.  
  217.     def back() :
  218.         root_reg.destroy()
  219.         main()
  220.  
  221.     #==================================== Buttons ================================
  222.  
  223.     btnBack = tk.Button(root_reg , font=('arial', 16, 'bold'), text='<< Go Back' , command = back , bd = 7)
  224.     btnBack.grid(row = 10 , column=0)
  225.  
  226.     btnRegister = tk.Button(root_reg , font=('arial', 16, 'bold'),
  227.                          text='Register' , command = reg , bd = 7)
  228.     btnRegister.grid(row=10, column=2)
  229.  
  230.     btnExit = tk.Button(root_reg , font=('arial', 16, 'bold'), text='Exit' , command = Exit , bd = 7)
  231.     btnExit.grid(row = 10, column = 4)
  232.  
  233.     root_reg.mainloop()
  234.  
  235. #=================================================================================
  236.  
  237. def login(root) :
  238.     root.destroy()
  239.  
  240.     root_login = tk.Tk()
  241.     root_login.title("Login")
  242.     #root_login.iconbitmap("icon.ico")
  243.     root_login.group()
  244.  
  245.  
  246.     username = tk.StringVar(root_login)
  247.     password = tk.StringVar(root_login)
  248.  
  249.     #====================================== Widgets ==============================
  250.  
  251.     lblUsername = tk.Label(root_login , font = ('arial' , 16 , 'bold') ,
  252.                            text = 'Enter Username')
  253.     lblUsername.grid(row = 0 , column = 0)
  254.     txtUsername  = tk.Entry(root_login , font = ('arial' , 13 , 'bold') , bd = 7,
  255.                             textvariable = username , insertwidth = 2)
  256.     txtUsername.grid(row = 0 , column = 2)
  257.  
  258.     lblPassword = tk.Label(root_login , font = ('arial' , 16 , 'bold') ,
  259.                            text = 'Enter Password')
  260.     lblPassword.grid(row = 2 , column = 0)
  261.     txtPassword  = tk.Entry(root_login , font = ('arial' , 13 , 'bold') , bd = 7,
  262.                             textvariable = password , insertwidth = 2 , show = '*')
  263.     txtPassword.grid(row = 2 , column = 2)
  264.  
  265.     #==================================== Fetching Data from MySQL ===============
  266.  
  267.     def LOGIN() :
  268.         if username.get() == '' :
  269.             txtUsername.configure(bg = 'red' , fg = 'white')
  270.             messagebox.showerror("Login" , "You've left the Username field EMPTY!!!  All fields are MUST.")
  271.         elif password.get() == '' :
  272.             txtPassword.configure(bg = 'red' , fg = 'white')
  273.             messagebox.showerror("Login" , "You've left the Password field EMPTY!!!  All fields are MUST.")
  274.         else :
  275.             passphrase = sha512(f"{password.get()}".encode('utf-32')).hexdigest()
  276.             cur.execute(f"select * from users where username = '{username.get()}' and password = '{passphrase}';")
  277.             L1 = cur.fetchall()
  278.             if any(L1) :                
  279.                 root_login.destroy()
  280.                 MainWindow(username.get())
  281.             else : messagebox.showerror("Login" , "Such a User DOESNOT Exist!!\nPlease Check whether the Entered Details are correct or not;\nor REGISTER Yourself")
  282.  
  283.  
  284.     def Exit() :
  285.         iExit = messagebox.askyesno('Login' , 'Do you want to quit ?')
  286.         if iExit > 0 :
  287.             root_login.destroy()
  288.             return
  289.  
  290.     def back() :
  291.         root_login.destroy()
  292.         main()
  293.  
  294.     #==================================== Buttons ================================
  295.  
  296.     btnBack = tk.Button(root_login , font=('arial', 16, 'bold'), text='<< Go Back' , command = back , bd = 7)
  297.     btnBack.grid(row = 4 , column=0)
  298.  
  299.     btnLogin = tk.Button(root_login , font=('arial', 16, 'bold'), text='Login' , command = LOGIN , bd = 7)
  300.     btnLogin.grid(row=4, column=2)
  301.  
  302.     btnExit = tk.Button(root_login , font=('arial', 16, 'bold'), text='Exit' , command = Exit , bd = 7)
  303.     btnExit.grid(row = 4 , column = 4)
  304.    
  305.     root_login.mainloop()
  306.  
  307. #=================================================================================
  308.  
  309. def main() :
  310.     root = tk.Tk()
  311.     root.title("Password Manager")
  312.     #root.iconbitmap("icon.ico")
  313.     root.geometry("555x170")
  314.     root.group()
  315.     #root.configure(bgpic = 'icon.ico')
  316.  
  317.     #============================= Buttons and Widgets ===========================
  318.  
  319.     lbltitle = tk.Label(root , font = ('Times new Roman' , 24 , 'bold') ,
  320.                            text = '                Welcome To')
  321.     lbltitle.grid(row = 0 , column = 0)
  322.  
  323.     lbltitle2 = tk.Label(root , font = ('Times new Roman' , 24 , 'bold') ,
  324.                            text = '                Password Manager\n')
  325.     lbltitle2.grid(row = 1 , column = 0)
  326.  
  327.     btnRegister = tk.Button(root , font=('arial', 16, 'bold'), text='Register' , command = lambda : register(root) , bd = 7)
  328.     btnRegister.grid(row = 2 , column = 0 , sticky = 'w')
  329.  
  330.     btnLogin = tk.Button(root , font=('arial', 16, 'bold'), text='Login' , command = lambda : login(root) ,
  331.                          bd = 7 , width = 6)
  332.     btnLogin.grid(row=2 , column=1 , sticky = 'w')
  333.  
  334.     root.mainloop()
  335. main()
Add Comment
Please, Sign In to add comment