繁体   English   中英

tkinter中的PNG图像透明度

[英]PNG Image transparency in tkinter

我一直在尝试在 tkinter 中显示带有透明部分的.PNG 图像。 但我遇到了一个问题。 在显示图像时,它往往会失去透明度。 有没有办法解决这个问题?
我是 tkinter 的新手,但精通 pygame,我已经在 pygame 中应用了这个想法,它似乎有效。

这是我到目前为止编写的代码:

from tkinter import *
from tkinter import ttk
import tkinter.font as tkfont
from PIL import ImageTk,Image

root = Tk()
root.title("Kiara")
root.geometry("706x462")

#Images
home_button_image = ImageTk.PhotoImage(Image.open("E:/Kiara Project/Screens/home_button.png"))
note_button_image = ImageTk.PhotoImage(Image.open("E:/Kiara Project/Screens/note_button.png"))
about_button_image = ImageTk.PhotoImage(Image.open("E:/Kiara Project/Screens/about_button.png"))
menu_home_activated_image = ImageTk.PhotoImage(Image.open("E:/Kiara Project/Screens/menu_home_activated.png"))

#Buttons
home_button = Button(root, image=home_button_image, bd=0)
note_button = Button(root, image=note_button_image, bd=0)
about_button = Button(root, image=about_button_image, bd=0)

#Screens
menu_home_activated = Label(root, image=menu_home_activated_image, bd=0)

menu_home_activated.place(x=0, y=0)
note_button.place(x=300, y=50)

root.update()

root.mainloop()

这是 output: 在此处输入图像描述

menu_home_activated.png 与 tkinter window 大小相同,除了在 output 中可见的部分外是透明的。
如您所见,即使将 note_button 放置在透明部分中,它也不会显示。

如果有帮助,我正在使用 Python 3.8.3。

抱歉,但这有点太大了,无法发表评论,也不是完整的答案

我认为您不需要ImageTk. 尝试运行这个:(我不能,因为我不知道你想要它看起来像什么,而且我的电脑上没有这些文件)

from tkinter import * 
from tkinter.ttk import *  
root = Tk() 

# Creating photoimage objects to use in the buttons
home_button_image = PhotoImage(file = r"E:/Kiara Project/Screens/home_button.png")
note_button_image = PhotoImage(file = r"E:/Kiara Project/Screens/note_button.png")
about_button_image = PhotoImage(file = r"E:/Kiara Project/Screens/about_button.png")
menu_home_activated_image = PhotoImage(file = r"E:/Kiara Project/Screens/menu_home_activated.png")

# Making the buttons
home_button = Button(root, image = home_button_image).pack(side = TOP)
note_button = Button(root, image = note_button_image).pack(side = TOP)
about_button = note_button = Button(root, image = about_button_image).pack(side = TOP)

# Screens
lable = lable(root, image = menu_home_activated_image)
label.pack

root.update()

mainloop()

我不能保证这会起作用,但它会稍微清理一下代码,而且这有点匆忙,所以可能会有一些错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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