如何使 tkinter 中按钮小部件的 bg_color 透明

问题描述 投票:0回答:1
import customtkinter as ctk
from tkinter import *
from PIL import Image, ImageTk


def resize_image(event=None):
    width, height = window.winfo_width(), window.winfo_height()
    resized_image = image.resize((width, height))
    photo = ImageTk.PhotoImage(resized_image)
    background.configure(image=photo)
    background.image = photo
window = ctk.CTk()
width = 1280
height = 720
window.geometry(f"{width}x{height}")
window.resizable(False, False)
window.wm_attributes('-transparentcolor', '#ab23ff')
image = Image.open("testImages/istockphoto-1185382671-612x612.jpg")
photo = ImageTk.PhotoImage(image)

background = ctk.CTkLabel(window, image=photo, text="")

background.configure(image=photo)

background.bind("<Configure>", resize_image)
background.place(x=0, y=0)

#frame = ctk.CTkFrame(window, width=1250, height=690, bg_color="gray")
#frame.place(x=15, y=15)


btn = ctk.CTkButton(window, text="Start", width=150, corner_radius=10, border_width=0, bg_color="#000000")
btn.place(x=460, y=360)`

我尝试查看文档,但我发现似乎没有任何效果,并且文档中没有关于透明度的具体内容。

user-interface tkinter widget transparent customtkinter
1个回答
0
投票

如果您需要更多帮助,可以使用“pywinstyles”方法。它使某种颜色不可见,之后您可以在按钮中使用它。

`import pywinstyles

btn = ctk.CTkButton(window, text="Start", width=150, corner_radius=10, 
border_width=0, bg_color="#000001") #Your color that becomes invisible
btn.place(x=460, y=360)

pywinstyles.set_opacity(button, color="#000001") #You specify the color that 
will be invisible`
© www.soinside.com 2019 - 2024. All rights reserved.