如何让我的文本在 python 中出现然后消失?

How do I make my text appear and then disappear in python?

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

好吧,我想让文本只显示几秒钟(例如 2 秒),但每次我添加 time.sleep() 然后 canvas.delete() 它不会甚至显示文字 有人可以帮忙吗?

canvas.delete("all")用于删除所有对象在canvas.

只需要删除Label,所以可以把这段代码换成canvas.delete(redirecting_text),但是看不到Label,因为canvas会在time.sleep()之后重绘(更新)延迟。

为避免这种情况,您可以编写 canvas.update_idletasks(),但它不是 合适的解决方案。

 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)

不要在窗口应用程序中使用time.sleep(),因为调用此方法会冻结应用程序。

在 tkinter 中一段时间​​后做某事的正确方法是调用 after() 方法。像这样:

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)