簡體   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