我想將圖像合併到一個簡單的紙牌遊戲中,並且在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"
我澄清了。現在再看看。 –
你的新代碼中有兩個'file ='。 – Lafexlos
做另一個編輯,檢查出來。 –