- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以我在 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
最佳答案
我不知道总线错误,但你需要先创建一个 Tk 窗口,然后才能调用 PhotoImage。这个脚本对我有用-
import Image
import ImageTk
from Tkinter import Tk
window = Tk()
i = Image.open("image.png")
photo = ImageTk.PhotoImage(i)
关于Python PIL ImageTk.PhotoImage() 给我一个总线错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3177231/
我对 tkinter 很陌生,我注意到当我尝试将图片作为背景添加到窗口时,它不会显示。这是代码。 from tkinter import * root = Tk() def cheese():
我有两段代码,它们都应该创建包含黑色方 block 的 test.png。第一个这样做,但第二个返回一个透明的正方形。它们的区别在于第一个左边有明显的条纹,第二个没有。 第一个例子: root = T
发现问题 今天在使用imagetk.photoimage()显示图片时,当把包含该函数放在自定义函数里时,不能正常显示,移到函数为又可正常显示,所以想到可能是变量不是全局性的缘故,改为全局变量后果
我正在尝试在我的文本小部件中显示两个图像对象。最初,这两个物体都显示为空图像。经过一番研究,我了解到我需要保留对图像的引用。我在代码中添加了一行额外的代码。我现在看到第二张图像正确显示,但第一张图像仍
from tkinter import * root = Tk() photo = PhotoImage(file='blueface.png') label = Label(root, image=
目标是将以下代码减少到尽可能少的代码行(和尽可能少的字符)。它不需要是“可读的”甚至是“pythonic”的,只要简短而甜美即可。这些更多是为了好玩。 self.BlueEgg = PhotoImag
在我正在创建的简单游戏中,我目前将占位符矩形对象作为图形。我正在尝试用 Sprite 替换它们,但据我了解,Tkinter 不支持 PNG 或 alpha 透明度。我正在使用 Python 3.3,它
我有一个 Python 3 Tkinter 程序,该程序应该从特殊文件格式渲染图像。 from tkinter import * image = [] path = Tk() display = To
我正在尝试使用 tkinter,但这段代码不起作用,我想知道是否有人知道为什么,谢谢。 from tkinter import * window = Tk() window.title("tkinte
我一直在尝试使用 PIL 调整图像大小,然后使用 Tkinter 显示它们,但程序一直在崩溃,我已将问题隔离到下面的第二行: image = Image.open("0.gif") photo = I
我正在尝试在 Mac 10.6.8 上运行以下命令: Python 2.7.2 |EPD 7.1-2 (64-bit)| (default, Jul 27 2011, 14:50:45) [GCC 4
我需要调整图像大小,但我想避免 PIL,因为我不能让它在 OS X 下工作 - 不要问我为什么...... 无论如何,因为我对 gif/pgm/ppm 很满意,所以 PhotoImage 类对我来说没
我正在尝试显示视频,并且使用示例/解决方案来解决如何在 Tkinter 上播放视频的问题似乎并不能提供流畅和快速的渲染。 例如: frame = self.vid_cap.read() frame =
此代码有效: img = PhotoImage(file="Image.gif") Label(root, image=img).pack() 这个方法怎么行不通? Label(root, image
这个问题在这里已经有了答案: Why does Tkinter image not show up if created in a function? (5 个答案) 关闭 4 年前。 我正在尝试在
我正在将 numpy 数组(使用 OpenCV 加载的网络摄像头素材)转换为 Tkinter PhotoImage 对象以在 Tkinter GUI 上显示它们。但是,函数 PIL.ImageTk.P
当操作 PhotoImage 对象时: import tkinter as tk img = tk.PhotoImage(file="myFile.gif") for x in range(0,100
所以我在 macbook pro 上运行 python 2.6 并尝试用 python 编写代码以在 tkinter gui 上的标签中显示文件中的图像。该图像称为 image.png。当我使用这段代
我收到错误'PhotoImage instance has no attribute 'resize'',尽管有多个在线资源表明这是使用 PIL 调整图像大小的方法。有什么想法吗? 相关代码: Dea
我正在研究 Yolo3-4-PY 以使用 tkinter 实现它。 我到处查找,但无法解决问题。 当我运行程序时,会显示 Canvas ,但是当我单击“开始视频”( btton ) 时,出现以下错误:
我是一名优秀的程序员,十分优秀!