簡體   English   中英

如何使用Python,tkinter和PIL在背景(也就是圖像)上以透明膠片覆蓋圖像

[英]How can I overlay images with transparencies over a background (which is an image, too) using Python, tkinter, and PIL

如果應該放在上層的圖片具有透明性,但它們顯示為灰色正方形而不是所述透明性,則覆蓋背景。

我正在使用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()”函數,這是問題所在。

因此,幾乎這張照片概括了我的問題:

在此處輸入圖片說明

*春天已經是透明的,只是沒有那樣顯示。

請記住,稍后,我需要放置所有其他元素(例如碰撞等),因此,我認為“ Image Magic”(請參見上面的代碼)是行不通的。

*編輯:我已經嘗試過如PNG文件在第一個答案中所述,但是問題仍然存在。

使用png代替gif文件可以解決您的問題。 還要刪除未使用的軟件包:

 from PIL import Image, ImageTk

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM