问题
How can I import a PNG-file into graphics.py and use it as if it were created within graphics.py?
import graphics
from graphics import *
window = GraphWin("gameWindow", 800, 600)
myImage = "myimage.png"
myImage.draw(window)
window.mainloop()
回答1:
You can use the Image class. If the call as only two parameters, will assume that the second parameter is the file path of your file:
myImage = Image(Point(5,5), 'myimage.png')
The following works fine but I don't think Tkinter (what is used by graphics.py) supports png file format. I had to use a gif.
import graphics
from graphics import *
window = GraphWin("gameWindow", 800, 600)
myImage = Image(Point(5,5), "myimage.gif")
myImage.draw(window)
window.mainloop()
来源:https://stackoverflow.com/questions/19249859/importing-custom-images-into-graphics-py