You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除CustomTkinter中CTkButton的灰色背景边框?

问题:CTkButton圆角按钮灰色背景边框的移除方案

我在使用CustomTkinter的CTkButton控件时遇到问题:按钮周围存在灰色背景边框(已在附图中用箭头标记)。我知晓将窗口背景设为灰色时该边框会消失,但更倾向于使用图片作为窗口背景。目前仅找到将corner_radius设为0的解决方案,但希望保留20的圆角值以实现按钮的圆润造型,请问该如何移除灰色背景边框?目标按钮为bottone_crea_promemoria变量。

相关代码

from tkinter import *
from PIL import Image, ImageTk
import customtkinter
from ctypes import windll

def promemoria():
    print('viva la gnocca')

windll.shcore.SetProcessDpiAwareness(1)

with open('C:\Users\quaranta\AppData\Local\Programs\Python\Python310\Lib\site-packages\promemoria.txt') as file_testo:
    contenuto = file_testo.read()
print(contenuto)

if contenuto == '':
    win = Tk()
    #win.resizable(False,False)
    win.title('Promemoria')
    win.configure(bg='#2B2B2B')
    trasparente = PhotoImage(file="trasparente.png")
    win.iconphoto(True, trasparente)
    
    immagine_cancella = Image.open('dontforget.png')
    immagine_cancella = immagine_cancella.resize((600, 450))
    immagine_cancella = ImageTk.PhotoImage(image=immagine_cancella)
    sfondo = Label(win,image=immagine_cancella)
    sfondo.place(x=0,y=0)
    
    etichetta_promemoria = Label(win,text=' Promemoria ',font=('Courier',17),bg='orange',fg='black',relief='solid',borderwidth=1)
    etichetta_promemoria.grid(row=0,column=0,padx=(25,0),pady=(20,0),sticky=W)
    
    etichetta_titolo = Label(win,text=' Titolo ',font=('Courier',12),bg='#006653',fg='white',relief='solid',borderwidth=1)
    etichetta_titolo.grid(row=1,column=0,sticky=W,padx=(145,0),pady=(30,0))
    
    campo_titolo = Entry(win, relief='flat', borderwidth=4, font=('Courier', 12), width=30, highlightthickness=2,
    highlightbackground="black", highlightcolor="black")
    campo_titolo.grid(row=2,column=0,pady=(8,0))
    
    etichetta_descrizione = Label(win, text=' Descrizione ', font=('Courier', 12), bg='#006653', fg='white', relief='solid',borderwidth=1)
    etichetta_descrizione.grid(row=3, column=0, sticky=W,padx=(145,0),pady=(45,0))
    
    campo_descrizione = Entry(win, relief='flat', borderwidth=4, font=('Courier', 12), width=30, highlightthickness=2,
    highlightbackground="black", highlightcolor="black")
    campo_descrizione.grid(row=4, column=0, pady=(8, 0))
    
    etichetta_data = Label(win, text=' Data ', font=('Courier', 12), bg='#006653', fg='white', relief='solid',borderwidth=1)
    etichetta_data.grid(row=5, column=0, sticky=W,pady=(40,0),padx=(135,0))
    
    campo_data = Entry(win, relief='flat', borderwidth=4, font=('Courier', 12), width=10, highlightthickness=2,
    highlightbackground="black", highlightcolor="black")
    campo_data.grid(row=6, column=0,sticky=W,pady=(8, 0),padx=(110.5,0))
    
    etichetta_ore = Label(win, text=' Ore ', font=('Courier', 12), bg='#006653', fg='white', relief='solid',
    borderwidth=1)
    etichetta_ore.grid(row=5, column=0,pady=(40,0),padx=(400,0),sticky=W)
    
    campo_ore = Entry(win, relief='flat', borderwidth=4, font=('Courier', 12), width=10, highlightthickness=2,
    highlightbackground="black", highlightcolor="black")
    campo_ore.grid(row=6, column=0, sticky=W, pady=(8, 0), padx=(372.3, 0))
    
    bottone_crea_promemoria = customtkinter.CTkButton(master=win,text='Crea promemoria',text_font = ('Courier',16,'bold'),width=260,border_width=2,border_color='black',fg_color='green',text_color='white',corner_radius=20,hover_color='#006A00',command=promemoria)
    bottone_crea_promemoria.grid(row=7,column=0,pady=30,padx=170)
    
    win.eval('tk::PlaceWindow . center')
    campo_titolo.focus_set()
    win.mainloop()
else:
    win2 = Tk()
    win2.geometry('300x300')
    win2.mainloop()

解决方案

方法一:直接设置按钮背景为透明

CTkButton的灰色边框是控件默认继承父窗口背景色导致的,当父窗口用图片背景时,控件无法识别图片,就会显示默认灰色。给按钮添加bg_color="transparent"参数即可解决,同时保留圆角:

bottone_crea_promemoria = customtkinter.CTkButton(
    master=win,
    text='Crea promemoria',
    text_font=('Courier',16,'bold'),
    width=260,
    border_width=2,
    border_color='black',
    fg_color='green',
    text_color='white',
    corner_radius=20,
    hover_color='#006A00',
    command=promemoria,
    bg_color="transparent"  # 新增这一行
)

方法二:用透明Frame包裹按钮

如果方法一不生效,可以给按钮套一个透明的CTkFrame,再将按钮放入Frame中:

# 创建透明Frame
btn_frame = customtkinter.CTkFrame(master=win, bg_color="transparent", fg_color="transparent")
btn_frame.grid(row=7,column=0,pady=30,padx=170)

# 在Frame内创建按钮
bottone_crea_promemoria = customtkinter.CTkButton(
    master=btn_frame,
    text='Crea promemoria',
    text_font=('Courier',16,'bold'),
    width=260,
    border_width=2,
    border_color='black',
    fg_color='green',
    text_color='white',
    corner_radius=20,
    hover_color='#006A00',
    command=promemoria,
    bg_color="transparent"
)
bottone_crea_promemoria.pack()

原理说明

CustomTkinter控件默认会继承父容器的背景色,当父容器是带图片的Label时,控件无法读取图片像素作为背景,就会 fallback 到默认灰色。设置bg_color="transparent"会让控件背景完全透明,露出底层图片背景,同时保留圆角裁剪效果。

内容的提问来源于stack exchange,提问作者Forty966996

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 19:40:34