[Tutor] Displaying Pictures & Text
Wayne Werner
waynejwerner at gmail.com
Sat Nov 6 15:16:40 CET 2010
On Fri, Nov 5, 2010 at 11:53 PM, Patty <patty at cruzio.com> wrote:
> Hi Alan -
>
> I tried using ImageTk from the PIL library to display jpegs (and hopefully
> any picture type) instead of just gif as you suggested below. I read
> online that these these types of programs should
> be run from executables not in the interpreter but it crashes and I can't
> read the error. Here is the program:
>
> import Image
> import ImageTk
>
> fhdl = Image.open("C:\Users\StarShip\PyProgs\SuitGirl.jpg")
> ImageTk.PhotoImage(fhdl)
>
Before you convert to an ImageTk, you need to first create a toplevel
window:
import Image
import ImageTk
import Tkinter as tk
root = tk.Tk()
#put your code here
And that should work. You'll still have to add the widgets and what-nots so
you can present your image, but in order to create an image with ImageTk,
you need to have a toplevel window created.
That's what this is telling you
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\lib\site-packages\PIL\ImageTk.py", line 113, in __init__
self.__photo = apply(Tkinter.PhotoImage, (), kw)
File "C:\Python26\lib\lib-tk\Tkinter.py", line 3285, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python26\lib\lib-tk\Tkinter.py", line 3226, in __init__
raise RuntimeError, 'Too early to create image'
RuntimeError: Too early to create image
HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101106/45f95a1e/attachment.html>
More information about the Tutor
mailing list