Tkinter Python (PNG) 中的透明度

时间:2021-07-24 16:48:05

标签: python tkinter

在我的 python 程序中,我有一个标题图像,但是它的 png 背景显示为白色。

image

我的图像代码如下:

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)

我将如何使标题透明?

1 个答案:

答案 0 :(得分:-1)

您可以像这样使用 Canvas

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)

我们只是在变量中创建 Canvas canvas 并在其中添加图像 img

编辑: 根据OP代码:

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)

我们将标题图片放在第一位,背景放在最后。

我的输出: image

相关问题