Hey there.
I started this login and password system, and am nearly finished. However, when I made it so that if someone already has the username you wanted to select, it would produce a label saying that you have to choose another username (def file_add). But, after like 2 seconds I want to destroy this label, but cant seem to do it, as I get the error NoneType object has no attribute to after. Was wondering if anyone could help me. Heres the code:
import tkinter as tk
from tkinter import *
import time
import os
## Acts for all buttons with command HoverButton, changes the color when hovering
class HoverButton(tk.Button):
def __init__(self, master, **kw):
tk.Button.__init__(self,master=master,**kw)
self.defaultBackground = self["background"]
self.bind("<Enter>", self.on_enter)
self.bind("<Leave>", self.on_leave)
def on_enter(self, e):
self['background'] = self['activebackground']
def on_leave(self, e):
self['background'] = self.defaultBackground
## Callable procedure of the register page
def register():
global register_screen
register_screen = Toplevel(main)
register_screen.geometry("400x250")
register_screen.title("Register")
global username
global password
global username_entry
global password_entry
username = StringVar()
password = StringVar()
Label(register_screen, text = " Please enter details below", font = "Bahnschrift", width = "400", bg = "light grey").pack()
Label(register_screen, text = "").pack()
username_label = Label(register_screen, text = "Username", font = "Bahnschrift").pack()
username_entry = Entry(register_screen, textvariable = username)
username_entry.pack()
password_label = Label(register_screen, text = "Password", font = "Bahnschrift").pack()
password_entry = Entry(register_screen, textvariable = password, show = "*")
password_entry.pack()
Label(register_screen, text = "").pack()
HoverButton(register_screen, text = "Confirm", font = "Bahnschrift", activebackground = "light grey", command = file_add, width = "10", height = "2").place(x = "100", y = "190")
HoverButton(register_screen, text = "Exit", font = "Bahnschrift", activebackground = "light grey", command = register_screen.destroy, width = "10", height = "2").place(x = "210", y = "190")
## Callable procedure for the login page
def login():
global login_screen
login_screen = Toplevel(main)
login_screen.title("Login")
login_screen.geometry("400x250")
Label(login_screen, text = "Please enter login details below", font = "Bahnschrift", width = "400").pack()
Label(login_screen, text = "").pack()
global username_verify
global password_verify
username_verify = StringVar()
password_verify = StringVar()
global username_login_entry
global password_login_entry
Label(login_screen, text = "Username", font = "Bahnschrift").pack()
username_login_entry = Entry(login_screen, textvariable = username_verify)
username_login_entry.pack()
Label(login_screen, text = "").pack()
Label(login_screen, text = "Password", font = "Bahnschrift").pack()
password_login_entry = Entry(login_screen, textvariable = password_verify, show = "*")
password_login_entry.pack()
HoverButton(login_screen, text = "Confirm", font = "Bahnschrift", activebackground = "light grey", width = "10", height = "2", command = login_verify).place(x = "100", y = "190")
HoverButton(login_screen, text = "Exit", font = "Bahnschrift", activebackground = "light grey", width = "10", height = "2", command = login_screen.destroy).place(x = "210", y = "190")
## Verifiction of the login
def login_verify():
username1 = username_verify.get()
password1 = password_verify.get()
username_login_entry.delete(0, END)
password_login_entry.delete(0, END)
list_files1 = os.listdir()
if username1 in list_files1:
file1 = open(username1, "r")
verify = file1.read().splitlines()
if password1 in verify[1]:
login_success()
else:
password_not_correct()
else:
user_not_found()
## This will bring the user to the main.py game
def login_success():
#########################################################################
print("Success")
login_screen.destroy
## Will run if the username is correct but the password is wrong
def password_not_correct():
user_not_found()
## If the user is not found, they are asked to login again
def user_not_found():
Label(login_screen, text = "Please try again \n Username or Password is wrong").pack()
## Will run this if the username chosen already exists
## Adding the username and password to the external file from (def register)
def file_add():
username_add = username.get()
password_add = password.get()
if username_add in os.listdir():
username_multiple = Label(register_screen, text = "Username already been chosen", font = "Bahnschrift").pack()
username_multiple.after(1000, lambda: username_multiple.destroy())
username_entry.delete(0,END)
password_entry.delete(0,END)
else:
file = open(username_add, "a")
file.write(username_add + "\n")
file.write(password_add + "\n")
file.close()
username_entry.delete(0,END)
password_entry.delete(0,END)
Label(register_screen, text = "Success").pack()
register_screen.after(1500, lambda: register_screen.destroy())
## Initiates the tkinter program and sets the dimensions of the main screen
def main_screen():
global main
main = Tk()
main.title("Home")
main.geometry("400x250")
## Blue bar across the top
Label(main, text = "Welcome to My Cup Of Tea \n Choose whether to Register or Login", font = "Bahnschrift", width = "400", bg = "grey").pack()
Label(main, text = "", bg = "light grey", width = "57", height = "0").pack()
Label(main, text = "", bg = "light grey", width = "12", height = "15").place(x="0", y="55")
Label(main, text = "", bg = "light grey", width = "12", height = "15").place(x="310", y="55")
## Register and Login Buttons
HoverButton(main, text = "Register", font = "Bahnschrift", activebackground = "light grey", command = register, width = "30", height = "2").pack()
Label(main, text = "", width = "31", bg = "light grey").pack()
HoverButton(main, text = "Login", font = "Bahnschrift", activebackground = "light grey", width = "30", height = "2", command = login).pack()
Label(main, text = "", width = "31", bg = "light grey").pack()
HoverButton(main, text = "Exit", font = "Bahnschrift", activebackground = "light grey", command = main.destroy, width = "30", height = "1").pack()
Label(main, text = "", width = "31", height = "3", bg = "light grey").pack()
main.mainloop()
main_screen()