Python中的Sleep函数不允许更新以前的信息
嘿,我只是做了一个基本的登录程序,同时了解tkinter,但我遇到了一个问题。如果您运行以下代码,当您点击“登录”时,状态标签不会更新为绿色且“有效”,而登录按钮只是保持按下5秒钟,然后窗口就会被破坏Python中的Sleep函数不允许更新以前的信息,python,python-3.x,Python,Python 3.x,嘿,我只是做了一个基本的登录程序,同时了解tkinter,但我遇到了一个问题。如果您运行以下代码,当您点击“登录”时,状态标签不会更新为绿色且“有效”,而登录按钮只是保持按下5秒钟,然后窗口就会被破坏 import tkinter # import tkinter module from time import sleep # import sleep # define global variables for functions to access window, main_window
import tkinter # import tkinter module
from time import sleep # import sleep
# define global variables for functions to access
window, main_window, text_status, status, ent, ent2, ent3 = None, None, None, None, None, None, None
def get_input():
global window, text_status, status, ent, ent2, ent3
username = ent.get()
password = ent2.get()
verified_password = ent3.get()
num_digits = 0
for char in password:
if char.isdigit():
num_digits += 1
# loop repeats while:
# there are less than 2 numbers in the password
# the passwords length is less than 6 or greater than 10
if password == verified_password and num_digits >= 2 and 10 >= len(password) >= 6:
status["fg"] = "green"
text_status.set("Valid (%s, %s)" % (username, password))
# open userfile and save userinfo
with open("userfile.txt","a") as userfile:
userfile.write(username + " " + password + " " + verified_password + " ")
sleep(5)
window.destroy()
elif password != verified_password:
status["fg"] = "red"
text_status.set("Passwords don't match")
else:
status["fg"] = "red"
text_status.set("Not Valid")
print("Pass must be between 6 - 10 (inclusive) chars and contain 2 numbers. Note Password is case sensitive")
def new_user():
global window, main_window, text_status, status, ent, ent2, ent3
# close startup window
main_window.destroy()
window = tkinter.Tk() # create a new window
window.title("Login Screen") # specifies window title
#window.wm_iconbitmap('icon.ico') # changes icon
window.geometry("200x170") # specifies window size
#Username and password status label
text_status = tkinter.StringVar()
text_status.set("Not Valid")
status = tkinter.Label(window, textvariable=text_status, fg="red")
un_lbl = tkinter.Label(window, text="Username:") # create new label
ent = tkinter.Entry(window) # create a text entry widget
ps_lbl = tkinter.Label(window, text="Password:")
ent2 = tkinter.Entry(window)
v_ps_lbl = tkinter.Label(window, text="Verify Password:")
ent3 = tkinter.Entry(window)
btn = tkinter.Button(window, text="Login", command=get_input) # create a button
# pack (add) widgets into window
status.pack()
un_lbl.pack()
ent.pack()
ps_lbl.pack()
ent2.pack()
v_ps_lbl.pack()
ent3.pack()
btn.pack()
window.mainloop() # draw the window and start application
def login():
return
def startup():
global main_window
main_window = tkinter.Tk()
main_window.title("Welcome")
# main_window.geometry("500x100")
greeting = tkinter.Label(main_window, text="Hello please choose one of the following")
login_button = tkinter.Button(main_window, text="Login", command=login)
mk_acc_button = tkinter.Button(main_window, text="Make Account", command=new_user)
#greeting.pack()
#login_button.pack()
#mk_acc_button.pack()
greeting.grid(column=0, row=0, columnspan=2)
login_button.grid(column=0, row=1)
mk_acc_button.grid(column=1, row=1)
main_window.mainloop()
startup()
mainloop
没有线程化。这意味着任何sleep和其他deley都会影响循环的运行
我建议您使用某种异步计时器。这对我没有帮助,伙计。在窗口被释放之前需要等待一段时间。过一段时间后不要运行程序。user.after()那么你的问题就不够清楚了。