我在Tkinter上构建了一个项目中的两个窗口:1.登录/安全用户界面,接受登录名和密码。有一个“登录”按钮链接到类的方法,检查名称/密码对,如果登录数据正确,则返回True。2.主应用程序用户界面。
所以问题是:输入正确的用户名和密码并按下按钮“登录”(这部分工作正常,因为我进入了正确的IF部分,方法返回True)后,本应调用主UI窗口,但没有发生任何事情。手动关闭登录窗口后,Tkinter引发错误。
我的登录界面代码:
class Security:
def __init__(self):
# -------- Window arrangement ------------------- #
self.root = Tk()
self.root.title("Gym Activity Tracker")
self.root.minsize(600, 300)
self.security_passed = False
self.user_name = None
# -------- IMG/(Banner) placing ----------------- #
self.canvas = Canvas(width=600, height=156)
self.image = PhotoImage(file="images/divide.png")
self.canvas.create_image(300, 78, image=self.image)
self.canvas.config(highlightthickness=0)
self.canvas.grid(column=0, row=0, columnspan=4)
# --------- Login labels ---------- #
self.user_label = Label(text="User: ")
self.user_label.grid(column=0, row=1, pady=(20, 5))
self.password_label = Label(text="Password: ", padx=50)
self.password_label.grid(column=0, row=2, pady=(0, 10))
self.info_label = Label(text="(Optional)", anchor="e")
self.info_label.grid(column=2, row=2)
# --------- Login Buttons ---------- #
self.add_user = Button(text="➕ Add User", command=self.add_user)
self.add_user.grid(column=2, row=1, pady=(20, 0))
self.login_button = Button(text="Login", width=25, command=self.security_check)
self.login_button.grid(column=1, row=3)
# --------- Login Entries ---------- #
self.user_name_var = StringVar(self.root)
self.user_name_var.set("Choose an User")
self.user_drop_list = OptionMenu(self.root, self.user_name_var, "sdf")
self.user_drop_list.config(width=24)
self.user_drop_list.grid(column=1, row=1, pady=(20, 5))
self.password_entry = Entry(width=30)
self.password_entry.grid(column=1, row=2)
self.root.mainloop()
def security_check(self):
"""Method validates the user's name and password (if applied) entered
and returns True if security check has been passed"""
user_name_entered = self.user_name_var.get()
user_password_entered = self.password_entry.get()
data = pd.read_csv("settings/users_list.csv")
database_user_pass = data.loc[data.name == f"{user_name_entered}", "password"]
if (f"{user_name_entered}" in data["name"].values) and (str(database_user_pass[1]) == user_password_entered):
return True
elif f"{user_name_entered}" not in data["name"].values:
tkinter.messagebox.showwarning(
title="Username error",
message="User does not exist. Please check a spelling or create a new user"
)
return False
elif str(database_user_pass) == user_password_entered:
tkinter.messagebox.showwarning(
title="Password error",
message="Password is not correct for this user"
)
return False
else:
print("Something went wrong with Security Check")
return False
它可以从www.example.com上运行main.py:
from frames.security import Security
from frames.ui import MainInterface
def main():
authorization = Security()
if authorization.security_check():
app = MainInterface()
想法是通过运行security_check()方法对用户进行授权,如果成功,则打开主用户界面。但却得到错误:
Traceback (most recent call last):
File "C:\Users\kosan\OneDrive\Documents\Python\Projects\personal-gym-tracker\main.py", line 17, in <module>
main()
File "C:\Users\kosan\OneDrive\Documents\Python\Projects\personal-gym-tracker\main.py", line 6, in main
if authorization.security_check():
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\kosan\OneDrive\Documents\Python\Projects\personal-gym-tracker\frames\security.py", line 106, in security_check
user_password_entered = self.password_entry.get()
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\kosan\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 3099, in get
return self.tk.call(self._w, 'get')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_tkinter.TclError: invalid command name ".!entry"
提前感谢您的帮助!
我尝试将.destroy()方法添加到security_check()方法的IF部分。预期.destroy()将关闭Security类的主循环并允许MainInterface类中的新主循环运行,但未成功。由于某种原因,它引发了与self.password_entry.get()相关的错误,但在www.example.com中未调用此值main.py,并且在该行调用“if www.example.com _检查()"。
1条答案
按热度按时间s5a0g9ez1#
我不确定如何解决您的问题,但您可以在退出UI后尝试使用“sys.exit(1)”