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

35

我想改变我的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 widget 的边框颜色?

2个回答

53

只需使用即可

widget.config(highlightbackground=COLOR)

此外,如果你不想要边框,将highlightthickness属性设置为0即可。


5
我希望这个好的解决方案适用于所有小部件,但例如“Combobox”小部件没有“highlightbackground”选项。因此,将此解决方案应用于组合框会引发“tkinter.TclError:unknown option“-highlightbackground””。 - Victor
11
highlightbackground与背景边框有什么关系,它应该即使在小部件未被突出显示时也能起作用? - Marjan100
@Marjan100 有什么建议吗?我也遇到了同样的问题。我需要更改边框颜色而不是 highlightbackground - TheLizzard
@Marjan100 找到了一个答案。将其放入带有 bg=... 的框架中,然后在小部件上使用 .pack(padx=..., pady=...) - TheLizzard

4

您需要设置两个高亮(有焦点和无焦点)以具有连续的颜色。

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

它在Ubuntu中运行得非常好,但是当我尝试在Windows中使用相同的代码时,它不起作用,只会在周围产生黑色边框,请问您能否提供任何答案?self.historyButton = Button(self.M_Frame, text='历史记录', activebackground='#0892d0', activeforeground='white', bd=0, relief='solid', bg='#222222', fg='white', highlightbackground='#0892d0', highlightcolor='#0892d0', font=('Flux Regular', 10, 'bold'), command=self.logHistoryToConsole) self.historyButton.grid(row=0, column=0, padx=2) - Hackytech

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接