求助:MacOS Ventura下Tkinter按钮阴影无法消除问题
MacOS Ventura 下 Tkinter 按钮阴影无法去除的解决思路
问题描述
在MacOS Ventura系统中使用Tkinter创建按钮时,即便设置了highlightthickness=0和borderwidth=0,且已将Python、pip、Tkinter更新至最新版本,按钮仍会带有阴影;但相同代码在其他设备上可正常运行。
代码示例
from tkinter import * win = Tk() # settings window_height = 500 window_width = 500 # window win.title("Polygon Selector") win.resizable(False, False) win.geometry(str(window_width) + "x" + str(window_height)) win.configure(bg="#F7F7F7") can = Canvas(win,width=window_width,height=window_height,bg="#F7F7F7",highlightthickness=0) can.place(x=0,y=0) # content label1 = Label(win,text="Here you can create different polygons\n!! HAVE FUN !!", fg="#FFB26B", bg="#F7F7F7") label1.pack() button = Button(win, text="To the selection", bg="#000000",highlightthickness=0, borderwidth=0) button.pack() win.mainloop()
可行解决思路
- 调整系统UI设置:MacOS Ventura的系统渲染规则可能强制给按钮添加阴影,可尝试以下操作:
- 打开「系统设置」→「辅助功能」→「显示」
- 勾选「降低透明度」,测试按钮阴影是否消失
- 若无效,再勾选「增强对比度」重新测试
- 用ttk.Style自定义按钮样式:ttk组件支持更灵活的样式定制,可绕过原生系统的按钮渲染逻辑:
from tkinter import ttk, Tk win = Tk() style = ttk.Style() # 定义无阴影按钮样式 style.configure('NoShadow.TButton', background='#000000', foreground='white', borderwidth=0, relief='flat') # 使用自定义样式创建按钮 button = ttk.Button(win, text="To the selection", style='NoShadow.TButton') button.pack() win.mainloop() - 更换Python环境:MacOS自带的Python可能与Ventura的UI框架存在兼容问题,尝试用Homebrew安装独立Python版本:
- 安装Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" - 安装Python:
brew install python - 使用该版本Python运行代码:
python3 your_script.py
- 安装Homebrew:
- 用Canvas绘制自定义按钮:完全绕过原生Button组件,手动绘制按钮外观并绑定事件:
from tkinter import Tk, Canvas, Label win = Tk() window_height = 500 window_width = 500 win.title("Polygon Selector") win.resizable(False, False) win.geometry(f"{window_width}x{window_height}") win.configure(bg="#F7F7F7") can = Canvas(win, width=window_width, height=window_height, bg="#F7F7F7", highlightthickness=0) can.place(x=0,y=0) label1 = Label(win, text="Here you can create different polygons\n!! HAVE FUN !!", fg="#FFB26B", bg="#F7F7F7") label1.pack() # 绘制自定义按钮 def on_button_click(event): print("按钮被点击") # 创建按钮矩形和文字 button_rect = can.create_rectangle(150, 100, 350, 140, fill="#000000", outline="") button_text = can.create_text(250, 120, text="To the selection", fill="white") # 绑定点击事件 can.tag_bind(button_rect, "<Button-1>", on_button_click) can.tag_bind(button_text, "<Button-1>", on_button_click) win.mainloop()
内容的提问来源于stack exchange,提问作者Enno
相关产品推荐
相关产品推荐

