我正在尝试使用 tkinter 和 customtkinter 创建一个带有登录页面的应用程序。用户使用有效凭据登录后,他们应该被带到他们的帐户页面。现在,它似乎按预期工作,但我也不断收到错误,我不明白为什么。
这是登录页面的代码:
class LoginPage:
def __init__(self) -> None:
self.root = ctk.CTk()
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("dark-blue")
self.root.after(0, lambda:self.root.state('zoomed'))
self.root.title("Login Page")
bgImage = Image.open("bg.jpg")
bg = ImageTk.PhotoImage(bgImage)
bgLabel = tk.Label(self.root, image = bg)
bgLabel.place(x = 0, y = 0, relwidth = 1, relheight = 1)
loginFrame = ctk.CTkFrame(self.root)
loginFrame.pack(pady = 120, padx = 500, fill = "both", expand = True)
loginLabel = ctk.CTkLabel(loginFrame, text="Login", font = ("Tahoma", 35, "bold"))
loginLabel.pack(pady = 30, padx = 10)
self.usernameEntry = ctk.CTkEntry(loginFrame, placeholder_text="Username", font = ("Tahoma", 15), width = 300, height = 50)
self.usernameEntry.pack(pady = 30, padx = 10)
self.passwordEntry = ctk.CTkEntry(loginFrame, placeholder_text="Password", show = "*", font = ("Tahoma", 15), width = 300, height = 50)
self.passwordEntry.pack(pady = 30, padx = 10)
loginButton = ctk.CTkButton(loginFrame, text="Login", font = ("Tahoma", 15, "bold"), command = self.validateLogin, width = 300, height = 50)
loginButton.pack(pady = 30, padx = 10)
self.root.mainloop()
def validateLogin(self) -> None:
name = self.usernameEntry.get().lower()
if self.checkProvidedLogin(name, self.passwordEntry.get()):
self.root.destroy()
AccountPage(name)
else:
messagebox.showerror(message = "Invalid login, Try again")
def checkProvidedLogin(self, username, password) -> bool:
with open('logins.csv', 'r') as file:
logins = file.read().splitlines()
for line in logins:
if line == username + "," + password:
return True
return False
if __name__ == "__main__":
LoginPage()
这是帐户页面的代码:
class AccountPage:
def __init__(self, username) -> None:
self.accountRoot = ctk.CTk()
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("dark-blue")
self.accountRoot.after(0, lambda:self.accountRoot.state('zoomed'))
bgImage = Image.open("bg.jpg")
bg = ImageTk.PhotoImage(bgImage)
bgLabel = tk.Label(self.accountRoot, image = bg)
bgLabel.place(x = 0, y = 0, relwidth = 1, relheight = 1)
accountFrame = ctk.CTkFrame(self.accountRoot)
accountFrame.pack(pady = 50, padx = 50, fill = "both", expand = True)
welcomeLabel = ctk.CTkLabel(accountFrame, text = f"Welcome, {username}!", font = ("Tahoma", 35, "bold"))
welcomeLabel.pack(pady = 20)
self.accountRoot.mainloop()
再次强调,该应用程序现在运行良好。当在登录页面输入有效的凭据时,帐户页面将按其应有的方式显示。但我也收到此错误消息:
invalid command name "2792697387712check_dpi_scaling"
while executing
"2792697387712check_dpi_scaling"
("after" script)
invalid command name "2792697396224update"
while executing
"2792697396224update"
("after" script)
invalid command name "2792696741952_click_animation"
while executing
"2792696741952_click_animation"
("after" script)
有什么想法可能导致这种情况吗?
我认为这可能与我在创建 AccountPage 实例之前在 validateLogin() 函数的登录页面上使用 destroy() 有关,但切换这些并没有真正帮助。
self.root.destroy()
要解决此问题,您可以隐藏窗口(也称为将窗口移到远离屏幕的地方)或制作“副本”并销毁它。长答案和解决方案。 将“启动”代码更改为此。它传递了接下来将使用的变量根
if __name__ == "__main__":
root = CTk()
LoginPage(root)
这是最重要的部分。你的函数应该是这样的。它将扎根并形成一个窗口。当然,现在将对象的父对象更改为
self.loginWindow
,您就可以 self.loginWindow.destroy()
,通过删除基本上所有内容的父对象,不会导致任何错误。
def __init__(self, root) -> None:
self.root = root
self.loginWindow = Toplevel(root)
set_appearance_mode("dark")
set_default_color_theme("dark-blue")
希望这有帮助