from tkinter import *
import time
root = Tk()
root.title("Login Form")
root.configure(bg="#9febe9")
root.geometry("700x500")
# Creating Canvases
canvas = Canvas(root, width=500, height=380, bg="white")
canvas.place(relx=0.14, rely=0.122, anchor=NW)
main_rectange = canvas.create_rectangle(475, 0, 501, 500, outline="white", fill="#4286f5")
header_text = canvas.create_text(40, 50, text="Account Login", fill="#557ed5", font="Times 20 bold", anchor=NW)
user_name_text = canvas.create_text(40, 112, text="Username", fill="#a7a7a9", anchor=NW, font="20")
username_entry = Entry(canvas, borderwidth=0, bg="#e5e5e5")
canvas.create_window(40, 140, window=username_entry, anchor=NW, height=30, width=400)
canvas.create_text(40, 190, text="Password", fill="#a7a7a9", anchor=NW, font="20")
password_entry = Entry(canvas, borderwidth=0, bg="#e5e5e5")
canvas.create_window(40, 210, window=password_entry, anchor=NW, height=30, width=400)
canvas.create_text(55, 255, text="Remember Me", anchor=NW, fill="#9a9d9c")
canvas.create_text(330, 255, text="Forgot Password?", anchor=NW, fill="#6d829f", font="bold 9")
login_button = Button(canvas, width=56, height=2, text="Log In", justify=CENTER, borderwidth=0, bg="#4286f5", fg="white", command=lambda: login_button())
canvas.create_window(40, 300, window=login_button, anchor=NW)
# Original Username and Password
original_username = "NightHawk510"
original_password = "ILOVECODING"
def login_button():
username = username_entry.get()
password = password_entry.get()
if username == original_username and password == original_password:
redirecting_text = canvas.create_text(40, 90, text="Credentials Match, Please Wait While We Redirect You To Your Vault", fill="red", anchor=NW)
time.sleep(1.5)
canvas.delete("all")
print("ESHTA")
else:
print("NOT ESHTA")
wrong_credentials_text = canvas.create_text(40, 90, text="Wrong Credentials, Try again", fill="red", anchor=NW)
username_entry.delete(0, END)
password_entry.delete(0, END)
root.mainloop()
Alright so I wanna make the texts only show up for a couple of seconds (2 for ex.) but everytime I add time.sleep() and then canvas.delete() It doesn't even show the text Can someone help?
CodePudding user response:
canvas.delete("all")
is used to delete all objects on canvas.
You need to delete Label only, so you can replace this code with canvas.delete(redirecting_text)
, but Label will not be seen, because canvas will be repainted (updated) after time.sleep()
delay.
To avoid this, you can write canvas.update_idletasks()
, but it is NOT a proper solution.
if username == original_username and password == original_password:
redirecting_text = canvas.create_text(40, 90, text="Credentials Match, Please Wait While We Redirect You To Your Vault", fill="red", anchor=NW)
canvas.update_idletasks()
time.sleep(1.5)
canvas.delete(redirecting_text)
Do not use time.sleep()
in windowed applications, because calling this method freezes an application.
The proper way to do something after some period of time in tkinter is to call after()
method. Something like this:
def hide_text(text):
canvas.delete(text)
def login_button():
username = username_entry.get()
password = password_entry.get()
if username == original_username and password == original_password:
redirecting_text = canvas.create_text(40, 90, text="Credentials Match, Please Wait While We Redirect You To Your Vault", fill="red", anchor=NW)
root.after(1500, lambda: hide_text(redirecting_text))
print("ESHTA")
else:
print("NOT ESHTA")
wrong_credentials_text = canvas.create_text(40, 90, text="Wrong Credentials, Try again", fill="red", anchor=NW)