我想将图像合并到一个简单的纸牌游戏中,并且在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
做另一个编辑,检查出来。 –