简体   繁体   中英

Transparency in Tkinter Python (PNG)

In my python program I have a title image, however it's png background is displaying as white.

图像

My code for my images is as followed:

from tkinter import *
from typing import final
import requests
from PIL import ImageTk, Image

root = Tk()

# get system display settings
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

path = 'C:/Users/Sam/Desktop/Clash Clan Manager/Graphics Based Program/Game Graphics/'

root.title("Clash Clan Manager")
root.iconbitmap(path + 'CCM logo circle.ico')
root.geometry(str(screen_width) + 'x' + str(screen_height-70))

# display title
titleImage= ImageTk.PhotoImage(Image.open(path + 'clash clan manager text.png'))
titleCanvas= Canvas(root, width= 1374, height= 396)
titleCanvas.pack()

titleCanvas.create_image(10,10,anchor=NW,image=titleImage)

# display background image
background = ImageTk.PhotoImage(Image.open(path + 'background image.png'))
backgroundCanvas = Canvas(root, width=screen_width, height = screen_height-70)
backgroundCanvas.pack()

backgroundCanvas.create_image(10,10, anchor=NW, image =background)

root.mainloop(0)

How would I go about making the title transparent?

You can use Canvas like this:

img= ImageTk.PhotoImage(Image.open(path + 'background image.png'))

canvas= Canvas(root, width= 600, height= 400)
canvas.pack()

#Add image to the Canvas Items
canvas.create_image(0,0,anchor=NW,image=img)

We are just creating Canvas canvas in variable and adding image img in it.

Edit: According to OP code:

titleImage= ImageTk.PhotoImage(Image.open(path + 'clash clan manager text.png'))
background = ImageTk.PhotoImage(Image.open(path + 'background image.png'))

canvas= Canvas(root, width= 600, height= 400)
canvas.pack()

canvas.create_image(0,0, image =titleImage)
canvas.create_image(0,<change position according to title image size or 0 if you want to put title in background image>, image =background)

We are putting titleImage first and background as last.

My output: 图像

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM