繁体   English   中英

tkinter 返回“没有这样的文件或目录”错误

[英]tkinter returns "no such file or directory" error

这是我的代码:

from tkinter import *

logoImage = PhotoImage(file='logo.png')
logoLabel = Label(root, image=logoImage, bg='dodgerblue3')
logoLabel.grid(row=0, column=0)

该图像与项目位于同一目录中。 事实上,当我用 cmd 或 Python 打开它时,它可以工作。 但是当我使用 VS Code 或 Turn it into.exe 文件时,它会显示这个错误:

Traceback (most recent call last):
  File "c:\Users\Simo\OneDrive\Python\Scientific calculator\calc.py", line 180, in <module>
    logoImage = PhotoImage(file='logo.png')
  File "C:\Users\Simo\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4093, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\Simo\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4038, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "logo.png": no such file or directory

我怎样才能解决这个问题?

也许尝试将文件放入您的工作区并使用相对路径。

第 2 行缺少两个,最后添加了root.mainloop() grid替换为pack

from tkinter import *


root = Tk()
logoImage = PhotoImage(file='logo.png')
logoLabel = Label(root, image=logoImage, bg='dodgerblue3')
logoLabel.pack()

root.mainloop()

Output:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM