Guest User

Untitled

a guest
Jul 2nd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. from tkinter import *
  2. import tkinter as tk
  3. import sqlite3
  4. import hashlib
  5. import os
  6. import weakref
  7.  
  8.  
  9. def main():
  10. root = Tk()
  11. width = 600 #sets the width of the window
  12. height = 600 #sets the height of the window
  13. widthScreen = root.winfo_screenwidth() #gets the width of the screen
  14. heightScreen = root.winfo_screenheight() #gets the height of the screen
  15. x = (widthScreen/2) - (width/2) #finds the center value of x
  16. y = (heightScreen/2) - (height/2) #finds the center value of y
  17. root.geometry('%dx%d+%d+%d' % (width, height, x, y))#places screen in center
  18. root.resizable(width=False, height=False)#Ensures that the window size cannot be changed
  19. filename = PhotoImage(file = 'Login.gif') #gets the image from directory
  20. background_label = Label(image=filename) #makes the image
  21. background_label.place(x=0, y=0, relwidth=1, relheight=1)#palces the image
  22. logins = login(root)
  23. root.mainloop()
  24.  
  25. class login(Tk):
  26. def __init__(self, master):
  27. self.__username = StringVar()
  28. self.__password = StringVar()
  29. self.__error = StringVar()
  30. self.master = master
  31. self.master.title('Login')
  32. userNameLabel = Label(self.master, text='UserID: ', bg=None, width=10).place(relx=0.300,rely=0.575)
  33. userNameEntry = Entry(self.master, textvariable=self.__username, width=25).place(relx=0.460,rely=0.575)
  34. userPasswordLabel = Label(self.master, text='Password: ', bg=None, width=10).place(relx=0.300,rely=0.625)
  35. userPasswordEntry = Entry(self.master, textvariable=self.__password, show='*', width=25).place(relx=0.460,rely=0.625)
  36. errorLabel = Label(self.master, textvariable=self.__error, bg=None, fg='red', width=35).place(relx=0.508,rely=0.545, anchor=CENTER)
  37. loginButton = Button(self.master, text='Login', command=self.login_user, bg='white', width=15).place(relx=0.300,rely=0.675)
  38. clearButton = Button(self.master, text='Clear', command=self.clear_entry, bg='white', width=15).place(relx=0.525,rely=0.675)
  39. self.master.bind('<Return>', lambda event: self.login_user()) #triggers the login subroutine if the enter key is pressed
  40.  
  41. def login_user(self):
  42. username = self.__username.get()
  43. password = self.__password.get()
  44. hashPassword = (password.encode('utf-8'))
  45. newPass = hashlib.sha256()
  46. newPass.update(hashPassword)
  47. if username == '':
  48. self.__error.set('Error! No user ID entered')
  49. elif password == '':
  50. self.__error.set('Error! No password entered')
  51. else:
  52. with sqlite3.connect ('pkdata.db') as db:
  53. cursor = db.cursor()
  54. cursor.execute("select userID, password from users where userID=?",(username,))
  55. info = cursor.fetchone()
  56. if info is None:
  57. self.__error.set('Error! Login details not found!')
  58. else:
  59. dbUsername = info[0]
  60. dbPassword = info[1]
  61. if username == dbUsername or newPass.hexdigest() == dbPassword:
  62. self.open_main_menu()
  63. self.master.withdraw()
  64. else:
  65. self.__error.set('Error! please try again')
  66. self.clear_entry()
  67.  
  68. def destroy(self):
  69. self.master.destroy()
  70.  
  71. def clear_entry(self):
  72. self.__username.set('')
  73. self.__password.set('')
  74.  
  75. def open_main_menu(self):
  76. root_main = Toplevel(self.master)
  77. root_main.state('zoomed')
  78. Main = main_menu(root_main)
  79. root_main.mainloop()
  80.  
  81.  
  82. class main_menu():
  83. def __init__(self, master):
  84. pass
  85.  
  86. if __name__ == '__main__':
  87. main()
  88.  
  89. import tkinter as tk
  90.  
  91. class Login(tk.Toplevel):
  92. def __init__(self, root):
  93. super().__init__(root)
  94.  
  95. self.root = root
  96.  
  97. username_label = tk.Label(self, text="Username:")
  98. password_label = tk.Label(self, text="Password:")
  99. username_entry = tk.Entry(self)
  100. password_entry = tk.Entry(self)
  101. submit = tk.Button(self, text="Login", command=self.login)
  102.  
  103. username_label.grid(row=0, column=0, sticky="e")
  104. username_entry.grid(row=0, column=1, sticky="ew")
  105. password_label.grid(row=1, column=0, sticky="e")
  106. password_entry.grid(row=1, column=1, sticky="ew")
  107. submit.grid(row=2, column=1, sticky="e")
  108.  
  109. def login(self):
  110. # if username and password are valid:
  111. self.withdraw()
  112. self.root.deiconify()
  113.  
  114. def main():
  115. root = tk.Tk()
  116. root.withdraw()
  117.  
  118. label = tk.Label(root, text="this is the main application")
  119. label.pack(fill="both", expand=True, padx=100, pady=100)
  120.  
  121. login_window = Login(root)
  122.  
  123. root.mainloop()
  124.  
  125. if __name__ == "__main__":
  126. main()
Add Comment
Please, Sign In to add comment