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() 它甚至不顯示文本有人可以幫忙嗎?
uj5u.com熱心網友回復:
canvas.delete("all")用于洗掉畫布上的所有物件。
你只需要洗掉 Label ,所以你可以用 替換這段代碼canvas.delete(redirecting_text),但是 Label 不會被看到,因為畫布會在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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/356582.html
上一篇:干凈地退出PythonTkinter應用程式,同時還在“after”回圈中的按鈕上使用“wait_variable”函式
