Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PhotoImage没有此类文件或目录错误_Python_Tkinter - Fatal编程技术网

Python PhotoImage没有此类文件或目录错误

Python PhotoImage没有此类文件或目录错误,python,tkinter,Python,Tkinter,因此,基本上,我正在尝试学习tkinter,但PhotoImage小工具无法工作 我的代码: import tkinter as tk root = tk.Tk() logo = tk.PhotoImage(file ="monky.gif") def test(): print("Hello World") text = tk.Label(root, text = "Hello World") text.pack() 我也试过: logo = tk.PhotoImage(fil

因此,基本上,我正在尝试学习tkinter,但PhotoImage小工具无法工作

我的代码:

import tkinter as tk

root = tk.Tk()
logo = tk.PhotoImage(file ="monky.gif")

def test():
    print("Hello World")

text = tk.Label(root, text = "Hello World")
text.pack()
我也试过:

logo = tk.PhotoImage(file ="/Users/MinecraftMaster/Desktop/Python/Tests/monky.gif")
错误消息:

Traceback (most recent call last):
  File "/Users/MinecraftMaster/Desktop/Python/Tests/Tkinter Test.py", line 4, in <module>
    logo = tk.PhotoImage(file ="monky.gif")
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 3539, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 3495, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "monky.gif": no such file or directory
回溯(最近一次呼叫最后一次):
文件“/Users/MinecraftMaster/Desktop/Python/Tests/Tkinter Test.py”,第4行,在
logo=tk.PhotoImage(file=“monky.gif”)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/_init__.py”,第3539行,在_init中__
图像。_u初始(自我,“照片”,名称,cnf,主机,**千瓦)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/_init__.py”,第3495行,在_init中__
self.tk.call(('image','create',imgtype,name,)+选项)
_tkinter.TclError:无法打开“monky.gif”:没有这样的文件或目录
这是我的目录的图片:


编辑:一个朋友帮我解决了这个问题。对于其他有此问题的人,只需将目录放入变量中,然后调用它。感谢所有试图提供帮助的人

您没有像标签一样放置包含照片的小部件,然后它就会出现

import tkinter as tk

root = tk.Tk()
photo = tk.PhotoImage(file = "monkey.gif")
label = tk.Label(image = photo).pack()
def test():
    print("Hello World")

text = tk.Label(root, text = "Hello World")
text.pack()
窗口的外观

@Omaro_IB。下图显示了我使用的脚本和结果。

您需要确保
monky.gif
文件与python脚本文件位于同一目录中,例如
monky.py

要显示图像,您需要使用
tk.Label
方法中的
image=logo
composite='top'
选项来告诉tkinter同时显示图像和文本,以便图像位于顶部

希望以上能有所帮助

更新:如何检查
monky.gif
文件与python脚本文件位于同一目录中。

打开终端,例如按Ctr+
Alt
+
T
。如果图像和python文件位于主文件夹/桌面/测试文件夹中,则在终端中键入
cd
命令以进入文件夹(即目录)。然后使用
ls
命令列出该文件夹中的文件。这些命令用于帮助您查看python文件(例如
monky.py
)和
monky.gif
文件位于同一目录中。如果这些文件不在同一文件夹中,则不会显示图像

$ cd Desktop/test_folder/
$ ls
monky.gif  monky.py

问题不在于小部件/语法,而是因为计算机找不到实际的文件。如果您看到错误,它会说:无法打开“monky.gif”:没有这样的文件或目录您的屏幕截图显示图像位于名为
Tkinter Test
的文件夹中。您的代码正试图从名为
Tests
的文件夹中加载它。当然失败了…谢谢你的尝试,但它仍然不能解决问题。我想的是Mac不支持这种重新引用目录的方式,我不知道…Mac支持的很好。事实上,当前工作目录中根本不存在文件
monky.gif
。目录可能是错误的,或者您想拼写它
monkey.gif
(或其他名称)。当您在试图运行脚本的同一提示符下键入命令
ls monky.gif
时会发生什么情况?@BryanOakley OP提供的图像清楚地显示了文件名是正确的并且它存在。@BlackThunder:OP显示文件存在于某个地方,但是没有显示它在当前工作目录中。在linux中,命令
ls
将列出发出命令的目录中的项目。您需要知道的另一个命令是用于更改目录的
cd
。要发出这些命令,您需要打开一个linux终端。在Ubuntu中,你可以按Ctrl键调用终端。请参阅上面的更新。“在linux中,执行ls命令以确保monky.gif文件与python脚本文件位于同一目录中。”这是一个错误的语句。该文件需要位于当前工作目录中。这可能与脚本的位置不同。@SunBear:您的答案仍然是错误的。相对路径(即:不以“/”开头的文件名)是相对于当前工作目录的,而不是相对于使用相对路径的脚本的。@BryanOakley我的脚本/答案不使用“/”而是相对于当前目录的“/”。@SunBear:对,相对于当前目录。这可能与脚本所在的目录不同。同样,相对路径是相对于当前工作目录的,但是您的回答说它是相对于带有脚本的目录的。包含脚本的目录可能是当前工作目录,也可能不是。