图形密码认证系统运行时触发Tkinter回调错误,请求排查
图形密码认证系统路径错误解决
问题说明
开发图形密码认证系统时,运行主类并点击按钮触发Tkinter回调错误,核心问题是系统找不到garbledImages路径。
主类代码
# 导入所需库 from tkinter import * from tkinter import ttk from tkinter import font import custom_button import garbled import obscure import password import segments def load_garbled(window, menu_frame): menu_frame.pack_forget() garbled.start(window) def load_obscured(window, menu_frame): menu_frame.pack_forget() obscure.start(window) def load_segmented(window, menu_frame): menu_frame.pack_forget() segments.start(window) def load_password(window, menu_frame): menu_frame.pack_forget() password.start(window) def start(win): win.geometry("1280x600") win.title("图形密码认证系统") menu_frame = Frame(win, height=600, width=1280) menu_frame.pack(fill='both', expand=1) label = Label(menu_frame, text="图形密码认证系统", font=('Freestyle Script', 54)) label.pack(padx=40, pady=30) btn_height = 90 btn_width = 450 btn_font = ('Trebuchet MS', 14) btn1 = custom_button.TkinterCustomButton(master=menu_frame, text="测试混淆图片", text_font=btn_font, height=btn_height, width=btn_width, corner_radius=10, command=lambda: load_garbled(win, menu_frame)).place(relx=0.3, rely=0.4, anchor=CENTER) btn2 = custom_button.TkinterCustomButton(master=menu_frame, text="测试分割图片", text_font=btn_font, height=btn_height, width=btn_width, corner_radius=10, command=lambda: load_segmented(win, menu_frame)).place(relx=0.3, rely=0.7, anchor=CENTER) btn3 = custom_button.TkinterCustomButton(master=menu_frame, text="测试模糊图片", text_font=btn_font, height=btn_height, width=btn_width, corner_radius=10, command=lambda: load_obscured(win, menu_frame)).place(relx=0.7, rely=0.4, anchor=CENTER) btn4 = custom_button.TkinterCustomButton(master=menu_frame, text="测试密码/图片认证", text_font=btn_font, height=btn_height, width=btn_width, corner_radius=10, command=lambda: load_password(win, menu_frame)).place(relx=0.7, rely=0.7, anchor=CENTER) win.mainloop() if __name__ == "__main__": win = Tk() start(win)
完整错误追踪信息
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\jalaj\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__ return self.func(*args) File "c:\Users\jalaj\Downloads\Python Course with Notes\2. Chapter 2\.vscode\python\Graphical-Password-Authentication-System-main\Graphical-Password-Authentication-System-main\custom_button.py", line 274, in clicked self.function() File "c:\Users\jalaj\Downloads\Python Course with Notes\2. Chapter 2\.vscode\python\Graphical-Password-Authentication-System-main\Graphical-Password-Authentication-System-main\main_menu.py", line 47, in <lambda> command=lambda: load_garbled(win, menu_frame)).place(relx=0.3, rely=0.4, File "c:\Users\jalaj\Downloads\Python Course with Notes\2. Chapter 2\.vscode\python\Graphical-Password-Authentication-System-main\Graphical-Password-Authentication-System-main\main_menu.py", line 13, in load_garbled garbled.start(window) File "c:\Users\jalaj\Downloads\Python Course with Notes\2. Chapter 2\.vscode\python\Graphical-Password-Authentication-System-main\Graphical-Password-Authentication-System-main\garbled.py", line 19, in start garbledImages = utils.getGarbledImages() File "c:\Users\jalaj\Downloads\Python Course with Notes\2. Chapter 2\.vscode\python\Graphical-Password-Authentication-System-main\Graphical-Password-Authentication-System-main\utils.py", line 23, in getGarbledImages imagePaths = os.listdir("garbledImages") FileNotFoundError: [WinError 3] 系统找不到指定的路径: 'garbledImages'
错误核心与解决办法
错误核心
utils.py中的getGarbledImages函数尝试读取garbledImages目录,但该目录不存在,或者代码使用的相对路径与实际运行目录不匹配。
解决办法
- 创建缺失目录:在项目根目录下手动新建
garbledImages文件夹,并放入测试用的混淆图片。 - 改用绝对路径:避免依赖相对路径的不确定性,修改
utils.py中的路径代码,示例:import os # 获取当前脚本所在目录的绝对路径 script_dir = os.path.dirname(os.path.abspath(__file__)) # 拼接目标目录的绝对路径 image_dir = os.path.join(script_dir, "garbledImages") imagePaths = os.listdir(image_dir) - 检查运行目录:确保运行Python脚本时,终端的工作目录是项目根目录,而非其他子目录。
内容的提问来源于stack exchange,提问作者joji
相关产品推荐
相关产品推荐

