如何使用Python,tkinter和PIL在背景(也是图像)上叠加透明胶片图像

时间:2016-05-03 23:03:16

标签: python image tkinter python-imaging-library

如果上层图片上的图片有透明度,但它们会显示灰色方块而不是所述透明度,覆盖背景。

我正在使用Tkinter和PIL在Python中编写一个弹球游戏。因为我需要在背景上显示游戏元素(脚蹼,保险杠等),所以当我试图覆盖它们时,它们会显示灰色方块而不是透明度有

这是代码:

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()

特别注意" JuegoNuevo()"功能,问题所在。

所以,差不多,这张照片总结了我的问题:

enter image description here

*弹簧已经透明,它只是没有显示出来。

请记住,稍后,我需要把所有其他元素,碰撞等等......所以,#34; Image Magic"事情(见上面的代码),我认为,不会工作。

*编辑:我已经尝试过第一个回答中提到的PNG文件,但问题仍然存在。

1 个答案:

答案 0 :(得分:0)

使用png代替gif文件可以解决您的问题。同时删除未使用的包:

 from PIL import Image, ImageTk