我想知道我应该如何设置平纹标签或按钮的边框颜色,我把发布在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()
下面是我想要更改的内容(现在的边框颜色是黑色,我想将其更改为红色):
发布于 2020-02-06 13:20:47
使用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上检查它,它在那里不工作。
发布于 2020-02-06 13:40:41
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()
不漂亮,但很管用。
发布于 2020-02-06 14:09:13
这可能会有帮助,因为我使用了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()
很长但很有效。
https://stackoverflow.com/questions/60095610
复制相似问题