ここ

2013-04-27 8 views
5

「イメージを作成するのは時期尚早では、」URLから画像を表示するための私のコードです:ここ

def display_image(image_url): 
    # create image 
    image = Image.open(StringIO(urllib2.urlopen(image_url).read())) 
    image = ImageTk.PhotoImage(image) 

    # create display 
    root = tk.Tk() 
    root.title('captcha') 
    root.geometry("%dx%d+%d+%d" % (image.width(), image.height(), 0, 0)) 

    # draw image 
    panel1 = tk.Label(root, image=image) 
    panel1.pack(side='top', fill='both', expand='yes') 
    panel1.image = image 

    # run display 
    root.mainloop() 

I取得私のコンソールに次の出力:

Traceback (most recent call last): 
    File "vote.py", line 119, in <module> 
    display_image(recaptcha.get_image_url(challenge_field)) 
    File "vote.py", line 89, in display_image 
    image = ImageTk.PhotoImage(image) 
    File "c:\Python27\lib\site-packages\PIL\ImageTk.py", line 113, in __init__ 
    self.__photo = apply(Tkinter.PhotoImage,(), kw) 
    File "c:\Python27\lib\lib-tk\Tkinter.py", line 3244, in __init__ 
    Image.__init__(self, 'photo', name, cnf, master, **kw) 
    File "c:\Python27\lib\lib-tk\Tkinter.py", line 3185, in __init__ 
    raise RuntimeError, 'Too early to create image' 
RuntimeError: Too early to create image 
Exception AttributeError: "PhotoImage instance has no attribute '_PhotoImage__poto'" in <bound method PhotoImage.__del__ of <PIL.ImageTk.PhotoImage instance at 0x0000000002EB8148>> ignored 

私は間違っていますか?どんな助けでも大歓迎です。

答えて

5

TkインスタンスはImageTkを使用して作成しています。最初にルート要素を常に作成し、残りのウィジェットを作成する必要があります。