Python 如何在画布项目中插入图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43009527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:25:18  来源:igfitidea点击:

How to insert an image in a canvas item?

pythontkintertkinter-canvas

提问by Milky_Way

I am working on a game project for school, which look like this : In-game aspect

我正在为学校做一个游戏项目,看起来像这样:游戏内方面

 In-game aspect

 游戏方面

I have created these colored polygons like this :

我像这样创建了这些彩色多边形:

ship = can.create_polygon(410,650,450,600,490,650 , fill= 'red' , outline='black')

ennemies = can.create_rectangle(x-r, y-r, x+r, y+r, fill='green')

So now i want to fill themwith my own image. Is that possible ? And how ?

所以现在我想用我自己的图像填充它们。那可能吗 ?如何 ?

回答by martineau

from tkinter import *

# create the canvas, size in pixels
canvas = Canvas(width=300, height=200, bg='black')

# pack the canvas into a frame/form
canvas.pack(expand=YES, fill=BOTH)

# load the .gif image file
gif1 = PhotoImage(file='small_globe.gif')

# put gif image on canvas
# pic's upper left corner (NW) on the canvas is at x=50 y=10
canvas.create_image(50, 10, image=gif1, anchor=NW)

# run it ...
mainloop()