GUI按钮问题

2024-05-16 13:52:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为我的python脚本创建一个GUI

我设法定义了一个背景,现在我想添加我的按钮,但问题是在这里。就像你在下面的屏幕上看到的

enter image description here

这是我的代码中定义图像的部分

root = tk.Tk()
root.wm_attributes("-topmost", True)
root.wm_attributes("-transparent", True)
root.config(bg='systemTransparent')
NewAccountImg=tk.PhotoImage(file="NewAccountImg.gif")
background_image=tk.PhotoImage(file="fond.png")
background_label=tk.Label(root, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

以及

root.newaccount = tk.Button(root,text="New Account",command=partial(Set_Acc_and_Hide,root),image=NewAccountImg)

如何使我的按钮透明


Tags: image脚本true定义guiroot按钮label
1条回答
网友
1楼 · 发布于 2024-05-16 13:52:38

你应该使用PIL库来处理图像

PIL支持透明

举个例子:

from PIL import Image, ImageDraw

img = Image.new('RGBA', (100, 100), (255, 0, 0, 0))

draw = ImageDraw.Draw(img)
draw.ellipse((25, 25, 75, 75), fill=(255, 0, 0))

img.save('test1.gif', 'GIF', transparency=0)
# this generates gif of red colored circle with white background(transparent background).

img.save('test2.gif', 'GIF', transparency=1)
# this generates gif of white colored circle(transparent circle) with red background. 

相关问题 更多 >