Python PIL ImageTk.PhotoImage() 给我一个总线错误?

发布于 2024-09-08 19:45:06 字数 1472 浏览 7 评论 0原文

所以我在 macbook pro 上运行 python 2.6 并尝试用 python 编写代码以在 tkinter gui 上的标签中显示文件中的图像。该图像称为 image.png。当我使用此代码时,程序运行没有错误,

i = Image.open("image.png")

但当我执行此代码时(我添加一行):

i = Image.open("image.png")
photo = ImageTk.PhotoImage(i)

程序将崩溃并在命令行中显示“总线错误”。我什至不知道这意味着什么。我认为 PIL 安装正确,因为 Image 可以工作,但 ImageTk 不能工作的事实让我困惑。谁能告诉我可能导致此总线错误的原因是什么?

编辑: 好吧,我编写了一个新程序来进一步测试错误。这是我运行的确切脚本:

import Image
import ImageTk

i = Image.open("image.png")
photo = ImageTk.PhotoImage(i)

现在,这是我的回溯,而不是“总线错误”。

Traceback (most recent call last):
  File "imageTest.py", line 5, in <module>
    photo = ImageTk.PhotoImage(i)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/ImageTk.py", line 113, in __init__
    self.__photo = apply(Tkinter.PhotoImage, (), kw)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 3285, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 3226, in __init__
    raise RuntimeError, 'Too early to create image'
RuntimeError: Too early to create image
Exception AttributeError: "PhotoImage instance has no attribute '_PhotoImage__photo'" in <bound method PhotoImage.__del__ of <ImageTk.PhotoImage instance at 0x3c7a30>> ignored

So I am running python 2.6 on a macbook pro and trying to write the code in python to display an image from a file in a label on a tkinter gui. The image is called image.png. The program runs without errors when I use this code

i = Image.open("image.png")

but when I do this code (I add one line):

i = Image.open("image.png")
photo = ImageTk.PhotoImage(i)

The program will crash and say "Bus error" in the command line. I don't even know what that means. I would think that PIL is installed correctly, since Image works, but the fact that ImageTk does not work puzzles me. Can anybody tell me what might be causing this Bus error?

EDIT:
Well I made a new program to test the error further. Here is the exact script I ran:

import Image
import ImageTk

i = Image.open("image.png")
photo = ImageTk.PhotoImage(i)

Now instead of getting "Bus error", this is my traceback.

Traceback (most recent call last):
  File "imageTest.py", line 5, in <module>
    photo = ImageTk.PhotoImage(i)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/ImageTk.py", line 113, in __init__
    self.__photo = apply(Tkinter.PhotoImage, (), kw)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 3285, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 3226, in __init__
    raise RuntimeError, 'Too early to create image'
RuntimeError: Too early to create image
Exception AttributeError: "PhotoImage instance has no attribute '_PhotoImage__photo'" in <bound method PhotoImage.__del__ of <ImageTk.PhotoImage instance at 0x3c7a30>> ignored

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云淡风轻 2024-09-15 19:45:06

我不知道总线错误,但您需要先创建一个 Tk 窗口,然后才能调用 PhotoImage。这个脚本对我有用-

import Image 
import ImageTk
from Tkinter import Tk

window = Tk()
i = Image.open("image.png") 
photo = ImageTk.PhotoImage(i)

I don't know about the Bus Error, but you need to create a Tk window before you can call PhotoImage. This script works for me-

import Image 
import ImageTk
from Tkinter import Tk

window = Tk()
i = Image.open("image.png") 
photo = ImageTk.PhotoImage(i)
流年里的时光 2024-09-15 19:45:06

ImageTk.PhotoImage 有一个垃圾收集(引用计数)错误。您必须将对 PhotoImage 对象的引用放置在类实例变量的全局变量中(例如 self.myphoto = ImageTk.PhotoImage(i))。

请参阅此警告:

http://infohost.nmt.edu/tcc /help/pubs/pil/image-tk.html

ImageTk.PhotoImage has a garbage collection (ref count) bug in it. You must place a reference to the PhotoImage object in either a global variable of a class instance variable (e.g., self.myphoto = ImageTk.PhotoImage(i)).

See this warning:

http://infohost.nmt.edu/tcc/help/pubs/pil/image-tk.html

海风掠过北极光 2024-09-15 19:45:06

即使您确实需要调用 Tk 窗口,您也需要设置目录以便它可以找到 image.png。

import os
import Image 
import ImageTk
from Tkinter import Tk

os.chdir('C:/../../') # put file path for the image.

window = Tk()
i = Image.open("image.png") 
photo = ImageTk.PhotoImage(i)

window.mainloop()

Even thought you do need to call a Tk window you also need to set the directory so that it can find the image.png.

import os
import Image 
import ImageTk
from Tkinter import Tk

os.chdir('C:/../../') # put file path for the image.

window = Tk()
i = Image.open("image.png") 
photo = ImageTk.PhotoImage(i)

window.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文