我有一個框架,可以在其中放置多個標簽。所有這些標簽都包含 png 影像,有些填充了整個背景,有些則沒有;有透明背景。我想獲得 16x16 像素的影像并將它們調整為 32x32 以提高可見性。我注意到調整大小后原始影像作業正常,但是當我將新影像添加到資產目錄并使用它時,其背景變為黑色。我有一個資產檔案夾,用于存盤我的 png 影像。我確信他們每個人都應該有透明的背景,至少根據我的照片編輯器。
這是最小的代碼:
from tkinter import *
from PIL import Image, ImageTk
import os
project_path = os.path.dirname(os.path.abspath(__file__))
root = Tk()
button_frame1 = Frame(root)
button_frame1.pack(side=TOP)
Label(button_frame1, text="Hotbar",padx=8).grid(row=0,column=2)
hotbarFrame = Frame(button_frame1, bg="white", width=288, height=32, highlightbackground="black", highlightthickness=1)
hotbarFrame.grid(row=0,column=3,padx=10)
hotbarItems = ["dirt", "grass_block_side", "grass", "stone", "cobblestone", "oak_planks", "oak_log", "oak_leaves2", "glass"]
hotbarImages, hotbarSlots = {}, []
for slot in range(len(hotbarItems)):
item = hotbarItems[slot]
item_file = open(project_path "\\assets\\blocks\\" item ".png", "rb")
hotbarImages[item] = ImageTk.PhotoImage(Image.open(item_file).resize((32,32), Image.NONE))
hotbarSlots.append(Label(hotbarFrame, image=hotbarImages[item], width=32, height=32, highlightthickness=1))
hotbarSlots[slot].grid(row=0,column=slot)
root.mainloop()
輸出:

uj5u.com熱心網友回復:
你應該使用這個代碼
hotbarSlots.append(Label(hotbarFrame, image=hotbarImages[item], width=32, height=32, highlightthickness=1 , bd=None))
或者可以將與該標簽的顏色相同的視窗背景相同
root.config(bg="white")
hotbarSlots.append(Label(hotbarFrame, image=hotbarImages[item], width=32,height=32, highlightthickness=1 , background="white"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/398953.html
標籤:Python 特金特 蟒蛇成像库 PNG python-3.9
上一篇:放置包含按鈕的框架
