首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在tkinter小部件中更改边框颜色?

如何在tkinter小部件中更改边框颜色?
EN

Stack Overflow用户
提问于 2020-02-06 13:00:34
回答 3查看 7.6K关注 0票数 0

我想知道我应该如何设置平纹标签或按钮的边框颜色,我把发布在solid和边框颜色将是黑色。我试过highlightthicknesshighlightcolorhighlightbackground,但是它不起作用

下面是我的代码示例:

代码语言:javascript
运行
复制
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()

下面是我想要更改的内容(现在的边框颜色是黑色,我想将其更改为红色):

图像

我试过你说的话,@,但它不起作用:图像

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-02-06 13:20:47

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

例如:

代码语言:javascript
运行
复制
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上检查它,它在那里不工作。

票数 1
EN

Stack Overflow用户

发布于 2020-02-06 13:40:41

AFAIK,没有办法在tkinter中改变边框的颜色。我使用的解决方法之一是在根中制作一个稍微大一点的标签,并将我的标签放入其中。

代码语言:javascript
运行
复制
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()

不漂亮,但很管用。

票数 0
EN

Stack Overflow用户

发布于 2020-02-06 14:09:13

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

我可以改变背景颜色。

代码语言:javascript
运行
复制
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()

很长但很有效。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60095610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档