在tkinter画布python上绘制png图像 [英] Drawing a png image on a tkinter canvas python

查看:67
本文介绍了在tkinter画布python上绘制png图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用画布小部件在 python 3.5 中使用 tkinter 创建一个简单的游戏.对于这个游戏,我需要能够使用透明(png)图像.这是我的代码:

I am trying to create a simple game using tkinter in python 3.5 using the canvas widget. For this game I am need to be able to use a transparent (png) image. Here is my code:

from PIL import ImageTk
from tkinter import Tk, Canvas

root = Tk()
im = ImageTk.PhotoImage(file="test.png")
canvas = Canvas(root, width=900, height=900)
canvas.pack()
canvasImage = canvas.create_image(0, 0, image=im, anchor="nw")
root.mainloop()

问题在于,尽管没有错误,但我无法加载具有透明背景的图像,但是却可以加载不具有透明背景的png图像.

The problem is that, despite getting no errors i can't load an image with a transparent background but i can load png images with no transparent background.

推荐答案

您应该尝试以下操作:

from tkinter import * 
root = Tk()
canvas = Canvas(root, width=500, height=500)
canvas.pack()
img = PhotoImage(file='path/your_image.png')
canvas.create_image(250, 250, image=img)
root.mainloop()

输出此处

这篇关于在tkinter画布python上绘制png图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆