问题
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()
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.
回答1:
You should try this:
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()
Output here
来源:https://stackoverflow.com/questions/46388292/drawing-a-png-image-on-a-tkinter-canvas-python