【问题标题】:How to change border color in tkinter widget? [duplicate]如何更改 tkinter 小部件中的边框颜色? [复制]
【发布时间】:2020-02-06 13:00:34
【问题描述】:

我想知道如何更改 tkinter Label 或 Button 的边框颜色,我将 releif 放在 solid 中,边框颜色将为黑色。我试过highlightthickness,highlightcolor,highlightbackground 但它不起作用

这是我的代码示例:

import tkinter as tk 

root = tk.Tk()
root.geometry("800x450")
root.title("How should i change border color")
tk.Label(root,text = "How should i change border color",width = 50 , height = 4 ,bg = "White",relief = "solid").place(x=10,y=10)
tk.Button(root,text = "Button",width = 5 , height = 1 ,bg = "White",relief = "solid").place(x=100,y=100)


root.mainloop()

这是我想要改变的(现在是黑色的边框颜色,我想把它改为红色):

image

我已经尝试过你所说的@moshe-perez,但它不起作用: image

【问题讨论】:

    标签: python tkinter border tk border-color


    【解决方案1】:

    当您使用highlightbackground 时,您需要提供颜色代码,例如"#37d3ff"。 所以使用highlightbackground="COLORCODE" 并删除relief="solid"

    例如:

    import tkinter as tk
    
    root = tk.Tk()
    root.geometry("800x450")
    root.title("How should i change border color")
    tk.Label(root, text="How should i change border color", width=50, height=4, bg="White", highlightthickness=4, highlightbackground="#37d3ff").place(x=10, y=10)
    tk.Button(root, text="Button", width=5, height=1, bg="White", highlightbackground="#37d3ff").place(x=100, y=100)
    
    
    root.mainloop()
    

    结果:

    更新:虽然它可以在我的 ubuntu 机器上运行,但我只是在 windows 上检查了它,但它在那里不起作用。

    【讨论】:

    • 对不起,它不起作用,边框仍然是黑色的,但样式不同
    • @A._.P._.G 嗯我测试了它,它可以在我的电脑上运行。也许它只适用于 Linux?
    • 我不知道,也许,你可以给我发张照片吗?我想看看它的样子
    • @A._.P._.G 添加截图
    • 谢谢,但在我的电脑里还是黑的
    【解决方案2】:

    这可能会有所帮助,因为我使用了Frame

    我可以改变背景颜色。

    import tkinter as tk 
    
    root = tk.Tk()
    root.geometry("800x450")
    root.title("How should i change border color")
    border = tk.Frame(root, background="green")
    label = tk.Label(border, text="How should i change border color", bd=5)
    label.pack(fill="both", expand=True, padx=5, pady=5)
    border.pack(padx=20, pady=20)
    button1 = tk.Button(root, background="green")
    name = tk.Button(button1, text="click", bd=0)
    name.pack(fill="both", expand=True, padx=2, pady=2)
    button1.pack(padx=20, pady=20)
    
    
    root.mainloop()
    

    冗长但有效。

    【讨论】:

    • 它可以用于标签,但是当您在另一个按钮下设置一个按钮时,如果用户单击该按钮,则边框(按钮 1)不会发生任何事情,并且当用户按下边框(按钮 1)时...跨度>
    【解决方案3】:

    AFAIK,tkinter 无法更改边框颜色。我使用的解决方法之一是在根目录中制作一个稍大的标签,然后将我的标签放入其中。

    import tkinter as tk
    
    root = tk.Tk()
    root.geometry("800x450")
    root.title("How should i change border color")
    border = tk.Label(root, width=52, height=5, bg='red')
    border.place(x=10, y=10)
    tk.Label(border, text="How should i change border color", width=50, height=4, bg="White", highlightthickness=4, highlightbackground="#37d3ff").place(x=1, y=1)
    tk.Button(root, text="Button", width=5, height=1, bg="White", highlightbackground="#37d3ff").place(x=100, y=100)
    
    
    root.mainloop()
    

    不漂亮,但它有效。

    【讨论】:

    • 谢谢丹尼尔,它是标签的好方法,但对于按钮来说,它不会像你说的那样漂亮
    • "AFAIK,tkinter 没有办法改变背景颜色" - 你的意思是 border 颜色吗?背景颜色很简单改变。
    • 是的,我做到了。对不起:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 2021-12-23
    相关资源
    最近更新 更多