SHARE
TWEET

Untitled

a guest Oct 10th, 2019 183 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Hey there. This is a password and login system that I have tried to create. However, I have a slight issue that I could use some
  2. ## help on. Line 150, there is a section of code that attempts to destroy a label that is produced when a user inputs a username
  3. ## that has already been used. However, I get an error stating that NoneType object has no attribute to after, and I was wondering
  4. ## if there is another way to show the label on Line 149, keep it there for the user to see (say like 2 seconds), and then destroy it.
  5.  
  6.  
  7. import tkinter as tk
  8. from tkinter import *
  9. import time
  10. import os
  11.  
  12. ## Acts for all buttons with command HoverButton, changes the color when hovering
  13.  
  14. class HoverButton(tk.Button):
  15.     def __init__(self, master, **kw):
  16.         tk.Button.__init__(self,master=master,**kw)
  17.         self.defaultBackground = self["background"]
  18.         self.bind("<Enter>", self.on_enter)
  19.         self.bind("<Leave>", self.on_leave)
  20.  
  21.     def on_enter(self, e):
  22.         self['background'] = self['activebackground']
  23.  
  24.     def on_leave(self, e):
  25.         self['background'] = self.defaultBackground
  26.  
  27.  
  28. ## Callable procedure of the register page
  29.  
  30. def register():
  31.    
  32.     global register_screen
  33.    
  34.     register_screen = Toplevel(main)    
  35.     register_screen.geometry("400x250")
  36.     register_screen.title("Register")
  37.  
  38.     global username
  39.     global password
  40.     global username_entry
  41.     global password_entry
  42.    
  43.     username = StringVar()
  44.     password = StringVar()
  45.  
  46.     Label(register_screen, text = " Please enter details below", font = "Bahnschrift", width = "400", bg = "light grey").pack()
  47.     Label(register_screen, text = "").pack()
  48.  
  49.     username_label = Label(register_screen, text = "Username", font = "Bahnschrift").pack()
  50.     username_entry = Entry(register_screen, textvariable = username)
  51.     username_entry.pack()
  52.  
  53.     password_label = Label(register_screen, text = "Password", font = "Bahnschrift").pack()
  54.     password_entry = Entry(register_screen, textvariable = password, show = "*")
  55.     password_entry.pack()
  56.        
  57.     Label(register_screen, text = "").pack()
  58.  
  59.     HoverButton(register_screen, text = "Confirm", font = "Bahnschrift", activebackground = "light grey", command = file_add, width = "10", height = "2").place(x = "100", y = "190")    
  60.     HoverButton(register_screen, text = "Exit", font = "Bahnschrift", activebackground = "light grey", command = register_screen.destroy, width = "10", height = "2").place(x = "210", y = "190")
  61.  
  62.  
  63. ## Callable procedure for the login page
  64.  
  65. def login():
  66.  
  67.     global login_screen
  68.  
  69.     login_screen = Toplevel(main)
  70.     login_screen.title("Login")
  71.     login_screen.geometry("400x250")
  72.  
  73.     Label(login_screen, text = "Please enter login details below", font = "Bahnschrift", width = "400").pack()
  74.     Label(login_screen, text = "").pack()
  75.  
  76.     global username_verify
  77.     global password_verify
  78.  
  79.     username_verify = StringVar()
  80.     password_verify = StringVar()
  81.  
  82.     global username_login_entry
  83.     global password_login_entry
  84.  
  85.     Label(login_screen, text = "Username", font = "Bahnschrift").pack()
  86.     username_login_entry = Entry(login_screen, textvariable = username_verify)
  87.     username_login_entry.pack()
  88.  
  89.     Label(login_screen, text = "").pack()
  90.  
  91.     Label(login_screen, text = "Password", font = "Bahnschrift").pack()
  92.     password_login_entry = Entry(login_screen, textvariable = password_verify, show = "*")
  93.     password_login_entry.pack()
  94.  
  95.     HoverButton(login_screen, text = "Confirm", font = "Bahnschrift", activebackground = "light grey", width = "10", height = "2", command = login_verify).place(x = "100", y = "190")
  96.     HoverButton(login_screen, text = "Exit", font = "Bahnschrift", activebackground = "light grey", width = "10", height = "2", command = login_screen.destroy).place(x = "210", y = "190")
  97.  
  98.  
  99. ## Verifiction of the login
  100.  
  101. def login_verify():
  102.  
  103.     username1 = username_verify.get()
  104.     password1 = password_verify.get()
  105.    
  106.     username_login_entry.delete(0, END)
  107.     password_login_entry.delete(0, END)
  108.  
  109.     list_files1 = os.listdir()
  110.    
  111.     if username1 in list_files1:
  112.         file1 = open(username1, "r")
  113.         verify = file1.read().splitlines()
  114.        
  115.         if password1 in verify[1]:
  116.             login_success()
  117.  
  118.         else:
  119.             password_not_correct()
  120.  
  121.     else:
  122.         user_not_found()
  123.  
  124.  
  125. ## This will bring the user to the main.py game
  126.        
  127. def login_success():
  128.    
  129. #########################################################################
  130.     print("Success")
  131.     login_screen.destroy
  132.  
  133.  
  134. ## Will run if the username is correct but the password is wrong
  135.  
  136. def password_not_correct():
  137.  
  138.     user_not_found()
  139. ## If the user is not found, they are asked to login again
  140.  
  141. def user_not_found():
  142.    
  143.     Label(login_screen, text = "Please try again \n Username or Password is wrong").pack()
  144.  
  145. ## Adding the username and password to the external file from (def register)
  146.    
  147. def file_add():
  148.  
  149.     username_add = username.get()
  150.     password_add = password.get()
  151.     if username_add in os.listdir():
  152.         username_multiple = Label(register_screen, text = "Username already been chosen", font = "Bahnschrift").pack()
  153.         username_multiple.after(1000, lambda: username_multiple.destroy())
  154.        
  155.         username_entry.delete(0,END)
  156.         password_entry.delete(0,END)
  157.  
  158.        
  159.     else:
  160.         file = open(username_add, "a")
  161.         file.write(username_add + "\n")
  162.         file.write(password_add + "\n")
  163.         file.close()
  164.  
  165.         username_entry.delete(0,END)
  166.         password_entry.delete(0,END)
  167.  
  168.         Label(register_screen, text = "Success").pack()
  169.  
  170.         register_screen.after(1500, lambda: register_screen.destroy())
  171.  
  172.    
  173. ## Initiates the tkinter program and sets the dimensions of the main screen
  174.    
  175. def main_screen():
  176.     global main
  177.    
  178.     main = Tk()
  179.     main.title("Home")
  180.     main.geometry("400x250")
  181.  
  182.  
  183.     ## Blue bar across the top
  184.     Label(main, text = "Welcome to My Cup Of Tea \n Choose whether to Register or Login", font = "Bahnschrift", width = "400",  bg = "grey").pack()
  185.     Label(main, text = "", bg = "light grey", width = "57", height = "0").pack()
  186.     Label(main, text = "", bg = "light grey", width = "12", height = "15").place(x="0", y="55")
  187.     Label(main, text = "", bg = "light grey", width = "12", height = "15").place(x="310", y="55")
  188.        
  189.     ## Register and Login Buttons
  190.     HoverButton(main, text = "Register", font = "Bahnschrift", activebackground = "light grey", command = register, width = "30", height = "2").pack()
  191.     Label(main, text = "", width = "31", bg = "light grey").pack()
  192.    
  193.     HoverButton(main, text = "Login", font = "Bahnschrift", activebackground = "light grey", width = "30", height = "2", command = login).pack()
  194.     Label(main, text = "", width = "31", bg = "light grey").pack()
  195.    
  196.     HoverButton(main, text = "Exit", font = "Bahnschrift", activebackground = "light grey", command = main.destroy, width = "30", height = "1").pack()
  197.     Label(main, text = "", width = "31", height = "3", bg = "light grey").pack()
  198.    
  199.     main.mainloop()
  200.  
  201. main_screen()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top