如何设置特定Tkinter组件的边框颜色?

36 投票
2 回答
101472 浏览
提问于 2025-04-16 07:51

我想改变我Tkinter应用的背景颜色,但有些小部件周围会留下一圈白色的边框。

比如说,这段代码:

from tkinter import *

COLOR = "black"

root = Tk()
root.config(bg=COLOR)

button = Button(text="button", bg=COLOR)
button.pack(padx=5, pady=5)
entry = Entry(bg=COLOR, fg='white')
entry.pack(padx=5, pady=5)
text = Text(bg=COLOR, fg='white')
text.pack(padx=5, pady=5)

root.mainloop()

我该怎么设置某些Tkinter小部件的边框颜色呢?

2 个回答

5

你需要设置两个高亮效果(有焦点和没有焦点的状态),这样颜色才能保持一致。

from tkinter import *
root = Tk()
e = Entry(highlightthickness=2)
e.config(highlightbackground = "red", highlightcolor= "red")
e.pack()
root.mainloop()
53

只需要使用

widget.config(highlightbackground=COLOR)

另外,如果你根本不想要那个边框,可以把 highlightthickness 这个属性设置为 0(零)。

撰写回答