2016-04-16 22 views
0

我使用python 2.7,由於某些原因它不能識別一些模塊。我想用Tkinter打印圖像,但它不起作用。問題與圖像在GUI python

from Tkinter import * 
 
import ImageTk 
 

 
root = Tk() 
 

 
frame = Frame(root) 
 
frame.pack() 
 

 
canvas = Canvas(frame, bg="black", width=500, height=500) 
 
canvas.pack() 
 

 
photoimage = ImageTk.PhotoImage(file="Logo.png") 
 
canvas.create_image(150, 150, image=photoimage) 
 

 
root.mainloop()

錯誤是:

C:\Python27\python.exe D:/Users/user-pc/Desktop/Appland/Project.py 
 
Traceback (most recent call last): 
 
    File "D:/Users/user-pc/Desktop/Appland/Project.py", line 2, in <module> 
 
    import ImageTk 
 
ImportError: No module named ImageTk 
 

 
Process finished with exit code 1

回答

0

ImageTkPIL模塊的一部分。

您需要使用from PIL import ImageTk

您還需要保存到你的圖片參考。這裏有一個例子。

photoimage = ImageTk.PhotoImage(file="Logo.png") 
root.image = photoimage 
canvas_image = canvas.create_image(150, 150, image=root.image) 
+0

這也是我的一個問題,當我嘗試「從PIL進口ImageTk」使用,因此它說:「沒有模塊名爲PIL」 – Nathan1982

+0

那麼你或許應該安裝PIL。 'pip安裝枕頭' – Pythonista

+0

我該怎麼做?在代碼中輸入它? – Nathan1982