对于那些需要知道的人,我正在运行 64 位 Ubuntu 12.04,并尝试使用安装了 pip 的 python3.2 运行有问题的脚本
对于我正在编写的项目,我想在 tkinter 窗口中显示图像。为此,我通过 pip 安装了 Pillow,并为 python 3 安装了 tkinter,如下所示:
pip-3.2 install pillow #install stuff here
sudo apt-get install python3-tk
然后我尝试运行以下脚本
import tkinter
from PIL import Image, ImageTk
root = tkinter.Tk()
i = Image.open(<path_to_file>)
p = ImaageTk.PhotoImage(i)
还有更多,但该块会引发错误。无论如何,当我尝试运行它时,我得到以下错误输出
/usr/bin/python3.2 "/home/anish/PycharmProjects/Picture Renamer/default/Main.py"
Traceback (most recent call last):
File "/usr/local/lib/python3.2/dist-packages/PIL/ImageTk.py", line 184, in paste
tk.call("PyImagingPhoto", self.__photo, block.id)
_tkinter.TclError: invalid command name "PyImagingPhoto"
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/anish/PycharmProjects/Picture Renamer/default/Main.py", line 26, in <module>
n = Main("/home/anish/Desktop/Images/")
File "/home/anish/PycharmProjects/Picture Renamer/default/Main.py", line 11, in __init__
self.PictureList = self.MakeImageList(self.dir)
File "/home/anish/PycharmProjects/Picture Renamer/default/Main.py", line 21, in MakeImageList
tkim = ImageTk.PhotoImage(temp_im)
File "/usr/local/lib/python3.2/dist-packages/PIL/ImageTk.py", line 123, in __init__
self.paste(image)
File "/usr/local/lib/python3.2/dist-packages/PIL/ImageTk.py", line 188, in paste
from PIL import _imagingtk
ImportError: cannot import name _imagingtk
Process finished with exit code 1
再多的谷歌搜索都没有给我一个解决方案——我发现的关于这个的主题通常是说重新安装 tkinter 和/或枕头。
这是我的 /usr/lib/python3.2/tkinter/ 的内容
['dialog.py', 'scrolledtext.py', 'simpledialog.py', 'tix.py', 'dnd.py', 'ttk.py', '__main__.py', '_fix.py', 'font.py', '__pycache__', 'messagebox.py', '__init__.py', 'commondialog.py', 'constants.py', 'colorchooser.py', 'filedialog.py']
这是我的 /usr/local/lib/python3.2/dist-packages/PIL/ 中的 [许多] 文件
['OleFileIO.py', 'ImageFileIO.py', 'ImageCms.py', 'GimpGradientFile.py', 'PSDraw.py', 'ImageDraw2.py', 'GimpPaletteFile.py', 'TiffImagePlugin.py', 'ImageChops.py', 'ImageShow.py', 'ImageStat.py', 'FliImagePlugin.py', 'ImageColor.py', 'XpmImagePlugin.py', 'ImageOps.py', 'ExifTags.py', 'FpxImagePlugin.py', 'PngImagePlugin.py', 'ImageFile.py', 'WalImageFile.py', 'PixarImagePlugin.py', 'PsdImagePlugin.py', '_util.py', 'ImageDraw.py', 'GribStubImagePlugin.py', 'ContainerIO.py', 'CurImagePlugin.py', 'JpegPresets.py', '_imagingft.cpython-32mu.so', '_imagingmath.cpython-32mu.so', 'PpmImagePlugin.py', 'BmpImagePlugin.py', 'XbmImagePlugin.py', 'DcxImagePlugin.py', 'PaletteFile.py', 'SunImagePlugin.py', 'BufrStubImagePlugin.py', 'JpegImagePlugin.py', 'SpiderImagePlugin.py', 'ImageEnhance.py', 'TgaImagePlugin.py', 'IcnsImagePlugin.py', 'MspImagePlugin.py', 'ImageSequence.py', 'GifImagePlugin.py', 'ImageTransform.py', 'FontFile.py', 'GbrImagePlugin.py', 'EpsImagePlugin.py', 'XVThumbImagePlugin.py', 'BdfFontFile.py', 'PcdImagePlugin.py', 'TarIO.py', 'FitsStubImagePlugin.py', 'ImageMode.py', 'ArgImagePlugin.py', 'IcoImagePlugin.py', '_imaging.cpython-32mu.so', 'McIdasImagePlugin.py', '_binary.py', '__pycache__', 'ImageQt.py', 'Hdf5StubImagePlugin.py', 'PalmImagePlugin.py', 'ImagePalette.py', 'WebPImagePlugin.py', 'ImageFont.py', 'ImagePath.py', 'TiffTags.py', 'ImImagePlugin.py', 'ImageWin.py', 'ImageFilter.py', '__init__.py', 'SgiImagePlugin.py', 'ImageTk.py', 'ImageMath.py', 'GdImageFile.py', 'WmfImagePlugin.py', 'PcfFontFile.py', 'ImageGrab.py', 'PdfImagePlugin.py', 'IptcImagePlugin.py', 'ImtImagePlugin.py', 'MpegImagePlugin.py', 'MicImagePlugin.py', 'Image.py', 'PcxImagePlugin.py']
你们能帮忙解决这个问题吗?我完全不确定,这让我困惑了几天。我在想可能是 Ubuntu 的 python3-tk 包不完整,但我看不出是这种情况。皮普的枕头也是如此。有任何想法吗?