2017-04-18 39 views
0

我想將圖像合併到一個簡單的紙牌遊戲中,並且在Python 3.6上使用Tkinter時,我得到的是非工作結果。我不確定我是否正確做這件事,但看一看。Tkinter的圖像(在Mac上)

from tkinter import * 

window = Tk() 
window.title('Test') 
canvas = Canvas(window, width = 500, height = 500) 
canvas.pack() 
myImage = PhotoImage(file='/Users/school/Documents/Python Projects/Images test/myImage.gif') 
canvas.create_image(0, 0, anchor = 100, image=myImage) 

不太確定這裏有什麼問題,因爲我的圖像只是一個簡單的圖像。感謝您的幫助!

編輯 我來到這裏的錯誤,_tkinter.TclError: couldn't recognize data in image file "./myImage.gif" 我確實有一個名爲myImage.gif

AGAIN EDITED我提出這個修改,使其更簡單的圖像。這是我現在的代碼。

import tkinter 

root = tkinter.Tk() 
canvas = tkinter.Canvas(root) 
canvas.grid(row = 0, column = 0) 
root.title('Test') 
photo = tkinter.PhotoImage(file = 'myImage.gif') 
canvas.create_image(0, 0, image=photo) 
root.mainloop() 

和我的錯誤是

Traceback (most recent call last): 
File "/Users/school/Documents/Python Projects/Images test/imageTest.py", line 7, in <module> 
photo = tkinter.PhotoImage(file = 'myImage.gif') 
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 3539, in __init__ 
Image.__init__(self, 'photo', name, cnf, master, **kw) 
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 3495, in __init__ 
self.tk.call(('image', 'create', imgtype, name,) + options) 
_tkinter.TclError: couldn't recognize data in image file "myImage.gif" 
+0

我澄清了。現在再看看。 –

+0

你的新代碼中有兩個'file ='。 – Lafexlos

+0

做另一個編輯,檢查出來。 –

回答

0

嗯,我是非常愚蠢的,並且發現我自己的問題。我用於PNG到.gif的轉換器是有缺陷的,所以它不是一個真正的.gif圖像。謝謝所有

+1

不要使用轉換器,直接使用PIL庫來使用png – abccd

+0

我在Python 3.6上,我不相信PIL支持3.6。 –

+0

它只是使用枕頭叉,它支持python 3.x,它是完全一樣的 – abccd