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

为何无法修改ttk.Entry组件的背景颜色?附测试代码

解决Windows 11下ttk.Entry背景色不生效的问题

在Windows系统的ttk组件中,background属性控制的是Entry的外部容器背景,而输入文本的区域背景由fieldbackground属性负责——这就是你设置background后输入区域还是白色的核心原因。

修改代码如下,将background替换为fieldbackground即可生效:

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
root.geometry('300x300')
s = ttk.Style(root)
s.configure(
    'TEntry',
    fieldbackground='blue',
    foreground='red'
)
s.map(
    'TEntry',
    foreground=[('focus', 'pink')],
    fieldbackground=[('focus', 'green')]
)
entry = ttk.Entry(root)
entry.pack()
root.mainloop()

额外提示:如果需要更自由的自定义样式,可以尝试使用s.theme_use('clam')切换主题,该主题是纯Tk实现,不受Windows原生视觉样式限制,但外观会和系统默认风格不同。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 23:35:25