Python tkinter 标签的边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39416021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Border for tkinter Label
提问by Pax Vobiscum
Not really relevant but i'm building a calendar and I have a lot of Label widgets, and therefore it will look alot nicer if I had some borders for them!
不是很相关,但我正在构建一个日历并且我有很多 Label 小部件,因此如果我为它们设置一些边框,它看起来会更好!
I have seen you can do this for other widgets such as Button, Entry and Text.
我已经看到您可以为其他小部件(例如按钮、条目和文本)执行此操作。
Minimal code:
最小代码:
from tkinter import *
root = Tk()
L1 = Label(root, text="This")
L2 = Label(root, text="That")
L1.pack()
L2.pack()
I have tried setting
我试过设置
highlightthickness=4
highlightcolor="black"
highlightbackground="black"
borderwidth=4
inside the widget, but still the same result.
在小部件内部,但结果仍然相同。
Is this even possible to do? Thank you!
这甚至可以做到吗?谢谢!
回答by Bryan Oakley
If you want a border, the option is borderwidth
. You can also choose the relief of the border: "flat"
, "raised"
, "sunken"
, "ridge"
, "solid"
, and "groove"
.
如果你想要一个边框,选项是borderwidth
。您还可以选择边框的浮雕:"flat"
,"raised"
,"sunken"
,"ridge"
,"solid"
,和"groove"
。
For example:
例如:
l1 = Label(root, text="This", borderwidth=2, relief="groove")
Note: "ridge"
and "groove"
require at least two pixels of width to render properly
注意:"ridge"
并且"groove"
需要至少两个像素的宽度才能正确渲染
回答by Xofo
@Pax Vobiscum - A way to do this is to take a widget and throw a frame with a color behind the widget. Tkinter for all its usefulness can be a bit primitive in its feature set. A bordercolor option would be logical for any widget toolkit, but there does not seem to be one.
@Pax Vobiscum - 一种方法是获取一个小部件并在小部件后面抛出一个带有颜色的框架。Tkinter 的所有有用性在其功能集中可能有点原始。边框颜色选项对于任何小部件工具包都是合乎逻辑的,但似乎没有。
from Tkinter import *
root = Tk()
topframe = Frame(root, width = 300, height = 900)
topframe.pack()
frame = Frame(root, width = 202, height = 32, highlightbackground="black", highlightcolor="black", highlightthickness=1, bd=0)
l = Entry(frame, borderwidth=0, relief="flat", highlightcolor="white")
l.place(width=200, height=30)
frame.pack
frame.pack()
frame.place(x = 50, y = 30)
An example using this method, could be to create a table:
使用此方法的一个示例可能是创建一个表:
from Tkinter import *
def EntryBox(root_frame, w, h):
boxframe = Frame(root_frame, width = w+2, height= h+2, highlightbackground="black", highlightcolor="black", highlightthickness=1, bd=0)
l = Entry(boxframe, borderwidth=0, relief="flat", highlightcolor="white")
l.place(width=w, height=h)
l.pack()
boxframe.pack()
return boxframe
root = Tk()
frame = Frame(root, width = 1800, height = 1800)
frame.pack()
labels = []
for i in range(16):
for j in range(16):
box = EntryBox(frame, 40, 30)
box.place(x = 50 + i*100, y = 30 + j*30 , width = 100, height = 30)
labels.append(box)