[英]How can I overlay images with transparencies over a background (which is an image, too) using Python, tkinter, and PIL
If the pics that are supossed to go on the upper layers have transparencies, but they show up with a gray square instead of the said transparency, covering the background. 如果应该放在上层的图片具有透明性,但它们显示为灰色正方形而不是所述透明性,则覆盖背景。
I am coding a pinball game in Python, using Tkinter and PIL. 我正在使用Tkinter和PIL用Python编写弹球游戏。 I'm stuck because I need to display the elements of the game (flippers, bumpers, etc) over a background, however, when I try to overlay them, they show up with a gray square, instead of the transparency they already have.
我之所以陷入困境是因为我需要在背景上显示游戏的元素(鳍,保险杠等),但是,当我尝试覆盖它们时,它们会显示为灰色正方形,而不是已经具有的透明度。
This is the code: 这是代码:
import sys
from tkinter import *
from PIL import Image, ImageTk
#Secci�³n funciones
def botones():
juego_Nuevo_Boton = Button( menu_Principal, text = ("Juego nuevo"), command = juegoNuevo, font = ("Arial Black", 10), width = 20).place(x = 15, y = 125 )
continuar_Juego_Boton = Button( menu_Principal, text = "Continuar juego", font = ("Arial Black", 10), width = 20) .place(x = 15, y = 160)
cerrar_El_Juego = Button( menu_Principal, text = "Cerrar el juego", command = menu_Principal.destroy, font = ("Arial Black", 10), width = 20).place(x = 15, y = 195 )
def juegoNuevo():
ventana_Juego_Nuevo = Toplevel()
ventana_Juego_Nuevo.geometry("600x700")
ventana_Juego_Nuevo.title("Nueva partida")
imagen_Fondo_Juego_Nuevo = PhotoImage( file = "fondo_Juego.gif" )
fondo_Juego_Nuevo = Label(ventana_Juego_Nuevo, image=imagen_Fondo_Juego_Nuevo)
fondo_Juego_Nuevo.place( x = 0, y = 0, relwidth = 1, relheight = 1 )
fondo_Juego_Nuevo.image = imagen_Fondo_Juego_Nuevo
imagen_lanzador_Canal = PhotoImage( file = "LanzadorCanal.gif" )
Lanzador_Canal = Label(ventana_Juego_Nuevo, image = imagen_lanzador_Canal)
Lanzador_Canal.place( x = 0, y = 0 )
Lanzador_Canal.image = imagen_lanzador_Canal
############################################
#I ALSO TRIED THIS BUT IT SHOWS UP SOME KIND OF IMAGE EDITOR CALLED "IMAGE MAGIC",
#I don't need to edit the pictures, i just need them overlayed, so i can finish the game.
#fondo_Del_Juego = Image.open("fondo_Juego.png")
#lanzador_Canal = Image.open("LanzadorCanal.png")
#fondo_Del_Juego.paste(lanzador_Canal, (0, 0), lanzador_Canal)
#fondo_Del_Juego.show()
#############################################
ventana_Juego_Nuevo.mainloop()
#Crear pantalla y titulo
menu_Principal = Tk()
menu_Principal.title("Pinball - Proyecto : creado por Daniel Bonilla")
menu_Principal.geometry("200x400")
imagen_Fondo_Menu = PhotoImage(file = "MenuPrincipal.gif")
fondo_Menu = Label(menu_Principal, image = imagen_Fondo_Menu)
fondo_Menu.place( x = 0, y = 0, relwidth = 1, relheight = 1 )
botones()
#Llamado a la ventana
menu_Principal.mainloop()
Pay special attention to "JuegoNuevo()" function, there's where the problem is. 要特别注意“ JuegoNuevo()”函数,这是问题所在。
So, pretty much, this picture sums up my problem: 因此,几乎这张照片概括了我的问题:
*The spring is transparent already, it just doesn't show up that way. *春天已经是透明的,只是没有那样显示。
Have in mind that, later on, I need to put all the other elements, collisions,etc... so, that "Image Magic" thing (see the code above), I think, won't work. 请记住,稍后,我需要放置所有其他元素(例如碰撞等),因此,我认为“ Image Magic”(请参见上面的代码)是行不通的。
*EDIT: I already tried as mentioned in the first answer with PNG files, but the problem is still there. *编辑:我已经尝试过如PNG文件在第一个答案中所述,但是问题仍然存在。
Use png
instead of gif
files will fix your problem. 使用
png
代替gif
文件可以解决您的问题。 Also remove the unused packages: 还要删除未使用的软件包:
from PIL import Image, ImageTk
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.