【问题标题】:How to set border color of certain Tkinter widgets?如何设置某些 Tkinter 小部件的边框颜色?
【发布时间】:2011-05-18 06:28:25
【问题描述】:

我正在尝试更改我的 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 小部件的边框颜色?

【问题讨论】:

    标签: python colors tkinter tcl border


    【解决方案1】:

    您必须将两个高光(有焦点和无焦点)设置为具有连续颜色。

    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 = 'History Logs', 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)
    【解决方案2】:

    随便用

    widget.config(highlightbackground=COLOR)
    

    此外,如果您根本不想要该边框,请将 highlightthickness 属性设置为 0(零)。

    【讨论】:

    • 我希望这个不错的解决方案适用于所有小部件,但例如 Combobox 小部件没有 highlightbackground 选项。所以应用于组合框的解决方案引发tkinter.TclError: unknown option "-highlightbackground"
    • highlightbackground 与背景边框有何关系,即使小部件未突出显示它也应该工作?
    • @Marjan100 有什么建议吗?我遇到了同样的问题。我需要更改边框颜色而不是 highlightbackground
    • @Marjan100 找到an answer。将其放入带有bg=... 的框架中,然后在小部件上使用.pack(padx=..., pady=...)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 2020-08-25
    • 1970-01-01
    相关资源
    最近更新 更多