所以,我正在使用tkinter在python中创建一个登录系统,我希望它在验证了电子邮件和密码后移动到另一个页面。我发现这样做的唯一方法是使用按钮单击命令。我只希望在验证电子邮件和密码后转到下一页。提前谢谢。
from tkinter import *
class login:
def __init__(self, master, *args, **kwargs):
self.emailGranted = False
self.passwordGranted = False
self.attempts = 8
self.label_email = Label(text="email:", font=('Serif', 13))
self.label_email.grid(row=0, column=0, sticky=E)
self.label_password = Label(text="password:", font=('Serif', 13))
self.label_password.grid(row=1, column=0, sticky=E)
self.entry_email = Entry(width=30)
self.entry_email.grid(row=0, column=1, padx=(3, 10))
self.entry_password = Entry(width=30, show="•")
self.entry_password.grid(row=1, column=1, padx=(3, 10))
self.login = Button(text="Login", command=self.validate)
self.login.grid(row=2, column=1, sticky=E, padx=(0, 10), pady=(2, 2))
self.label_granted = Label(text="")
self.label_granted.grid(row=3, columnspan=3, sticky=N+E+S+W)
def validate(self):
self.email = self.entry_email.get()
self.password = self.entry_password.get()
if self.email == "email":
self.emailGranted = True
else:
self.emailGranted = False
self.label_granted.config(text="wrong email")
self.attempts -= 1
self.entry_email.delete(0, END)
if self.attempts == 0:
root.destroy()
if self.password == "password":
self.passwordGranted = True
else:
self.passwordGranted = False
self.label_granted.config(text="wrong password")
self.attempts -= 1
self.entry_password.delete(0, END)
if self.attempts == 0:
root.destroy()
if self.emailGranted is False and self.passwordGranted is False:
self.label_granted.config(text="wrong email and password")
if self.emailGranted is True and self.passwordGranted is True:
self.label_granted.config(text="access granted")
// I want it to move on to PageOne here but I'm not sure how
class PageOne:
def __init__(self, master, *args, **kwargs):
Button(text="it works").grid(row=0, column=0)
if __name__ == "__main__":
root = Tk()
root.resizable(False, False)
root.title("login")
login(root)
root.mainloop()
答案 0 :(得分:0)
我还没有使用过tkinter,但转移到新页面就像是一个动作。并且在事件发生时执行操作。在您的情况下,事件是在电子邮件和密码通过验证之后。因此,您需要在验证成功后添加逻辑以移至新页面。
是的,你为什么要这样做?为什么不让用户按下按钮呢?答案 1 :(得分:0)
如果您想要这样做,只要有有效的电子邮件,它就会继续运行,那么您希望有一个函数可以获取参数(电子邮件)并返回一个布尔值(True或False )。要做到这一点,你需要这样做:
def function(email):
return email == <something>
要在键入时反复检查,请在循环中使用root.update()
而不是root.mainloop()
,这样您就可以执行以下操作:
while function(entryWidget.get()):
#all the configs for text
root.update()
#let them to the next page
这有用吗?