Opening images using tkinter on python 3 -
i made following (and short) code on python 3:
from tkinter import * pil import image, imagetk image = image.open("trollface.jpg") photo = imagetk.photoimage(image) canvas.create_image(0, 0, image = photo)
when run it, following error:
traceback (most recent call last): file "<stdin>", line 1, in <module> file "c:\winpython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile execfile(filename, namespace) file "c:\winpython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace) file "c:/comp sci/usb_virus/trollface_puzzle_picture.py", line 12, in <module> photo = imagetk.photoimage(image) file "c:\winpython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site-packages\pil\imagetk.py", line 112, in __init__ self.__photo = tkinter.photoimage(**kw) file "c:\winpython-64bit-3.4.3.4\python-3.4.3.amd64\lib\tkinter\__init__.py", line 3416, in __init__ image.__init__(self, 'photo', name, cnf, master, **kw) file "c:\winpython-64bit-3.4.3.4\python-3.4.3.amd64\lib\tkinter\__init__.py", line 3357, in __init__ raise runtimeerror('too create image') runtimeerror: create image
what doing terribly wrong?
you need create instance of tk
first:
root = tk()
from tkinter import * pil import image, imagetk root = tk() canvas = canvas(width=500, height=500, bg='white') canvas.pack() image = image.open("trollface.jpg") photo = imagetk.photoimage(image) canvas.create_image(250, 250, image=photo) root.mainloop()
the code above comes from here.
Comments
Post a Comment