【发布时间】:2020-10-31 04:45:15
【问题描述】:
我在这个 Tkinter 上苦苦挣扎,我阅读了与我的问题相关的所有主题。
问题是我有登录/注册页面,成功认证后,我想隐藏登录窗口,然后显示主窗口。我认为这是我的错误 - 我如何打开新窗口。
我给你一些代码来解释一下:
这是我的登录功能。 form_instance 是从另一个文件/脚本传递的登录/注册表单。
def handle_login(self, email, password, form_instance):
auth = self.firebase.auth()
user = auth.sign_in_with_email_and_password(email, password)
form_instance.destroy()
_app = FutBot()
这是加载图像的代码:
image = Image.open('./img/fut_bot_bg2.png')
background_image = ImageTk.PhotoImage(image)
background_label = ttk.Label(self.window, image=background_image)
background_label.photo = background_image
background_label.place(x=0, y=0, relwidth=1, relheight=1)
例外:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__
return self.func(*args)
File "D:\Projects\FutSnipeBot\Auth\auth_form.py", line 52, in handle_login
auth_handler.handle_login(email=self.email_input_field.get(), password=self.password_input_field.get(),
File "D:\Projects\FutSnipeBot\sign_log_pyre.py", line 59, in handle_login
_app = FutBot()
File "D:\Projects\FutSnipeBot\fut_bot.py", line 24, in __init__
self.configure_bot_gui()
File "D:\Projects\FutSnipeBot\fut_bot.py", line 41, in configure_bot_gui
image = Image.open('./img/fut_bot_bg2.png')
File "C:\Users\PC\AppData\Roaming\Python\Python39\site-packages\PIL\Image.py", line 2891, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: './img/fut_bot_bg2.png'
代码如下:
- 主入口文件(登录表单):pastebin.com/i7cCssR7
- 处理身份验证并打开主窗口的中间文件:pastebin.com/58Lvra0L
- 主窗口(我删除了与问题无关的功能):pastebin.com/4YMY4dSV
希望这能起到作用
在你说什么之前我想提一下,如果我运行 MainWindow,图像加载没有问题。
MainWindow 是一个类实现。处理登录的函数也是类实现。如果您需要更多代码或信息,请提及您想要的内容。
我希望有人能帮助我,因为我是 python 的初学者,使用这种语言似乎很愉快。
【问题讨论】:
-
如果 python 说它找不到文件,那就是事实:当前工作目录中不存在该文件。
-
好的,然后解释一下如果我只运行 MainWindow,而不从 Login 转换到 MainWindow,图像如何加载?这是关键——我认为这种转变是错误的,但不幸的是我不知道哪里出了问题。
-
路径是相对于py文件所在的位置
-
没有看到合适的minimal reproducible example,我们只能推测。也许某处的某些代码会更改工作目录。
-
@CoolCloud:查看linfo.org/current_directory.html 或在互联网上搜索当前工作目录。