Python在PhotoImage中退出
下面的代码使Python在尝试创建PhotoImage实例时“意外退出”(它打印1并退出)。我在OSX10.9.5上,使用ActiveState中的Python2.7.10和ActiveTcl 8.6.4,使用Run/Run模块从空闲状态运行脚本。有线索吗?我对Python和所有GUI模块都是新手Python在PhotoImage中退出,python,python-2.7,tkinter,python-imaging-library,Python,Python 2.7,Tkinter,Python Imaging Library,下面的代码使Python在尝试创建PhotoImage实例时“意外退出”(它打印1并退出)。我在OSX10.9.5上,使用ActiveState中的Python2.7.10和ActiveTcl 8.6.4,使用Run/Run模块从空闲状态运行脚本。有线索吗?我对Python和所有GUI模块都是新手 import numpy as np import collections import math import Tkinter from PIL import Image, ImageTk #
import numpy as np
import collections
import math
import Tkinter
from PIL import Image, ImageTk
# A root window for displaying objects
root = Tkinter.Tk()
# Convert the Image object into a TkPhoto object
im = Image.open('samples.png')
print 1
imgtk = ImageTk.PhotoImage(image=im)
print 2
# Put it in the display window
Tkinter.Label(root, image=imgtk).pack()
root.mainloop()
您是否尝试了以下链接:
import Tkinter
from PIL import Image, ImageTk
root = Tkinter.Tk()
imgtk = ImageTk.PhotoImage(file='test.jpg')
Tkinter.Label(root, image=imgtk).pack()
root.mainloop()
至少在某些系统上,包括在OS X上的Python2.7上,ImageTk.PhotoImage似乎已经崩溃。您可以改用Tkinter.PhotoImage,但它只将原始文件作为参数,这很可怜。您看到的错误消息是什么没有来自Python的消息;OSX说“Python意外退出”。我刚刚测试了您的代码,修改了imgtk行以反映上述答案。它在我的电脑上用Python2.7运行。似乎不需要带有image.open的行。它不适用于我。你在跑什么?你是怎么安装的?