Printing a label and then destroying it after a certain amount of time in Tkinter

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()

That is a lot of code, and only the most dedicated of mentors is going to sift through it. Perhaps you should put it on github instead and open a bug report on it. You’ll want to describe the problem in the bug as precisely as you can, as if you were reporting it to someone else and not yourself.

And let’s be honest: you’re probably going to be stuck solving it yourself, because not a lot of people are interested in Tkinter anymore. So to help yourself solve it, add comments to your bug as you go, outlining in plain language (doesn’t have to be code) the steps you’ve taken and what you’re going to try. When you’ve solved it, comment and close the bug.

Yes, it’s a lot of talking to yourself, but keeping a “dev diary” like this is important: it’s really easy to forget what you’ve tried and what you still need to try when you’re in a fit of debugging rage.

And sorry for the lecture. I seem to be in that mood today. If you do that github thing, I promise I’ll take a look :slight_smile:

Ok. Thankyou very much.

Huh, after seeing all those existing formatted sections, I assumed it was multiple files with a few flubbed up… so I suggested all that github business. I keep forgetting markdown has that 4-space formatting business.

@2_D, nevermind the github issue thing. I’ll take a look now. After wrangling PHP all week, python will be refreshing :slight_smile:

1 Like

Your error is a simple one: .pack() returns None, not the widget.

Just change this line:

username_multiple = Label(register_screen, text = "Username already been chosen", font = "Bahnschrift").pack()

To this:

username_multiple = Label(register_screen, text = "Username already been chosen", font = "Bahnschrift")

username_multiple.pack()

Oh my god. How stupid of me. Thankyou so much!

No problem. I spent two hours debugging an error last week because I misplaced a comma.

1 Like