Border for tkinter Label

Question:

I’m building a calendar that would look much nicer if it had some borders for its Labels!

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.

example pic tkinter

Is this even possible to do? Thank you!

Asked By: Pax Vobiscum

||

Answers:

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".

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

examples of tkinter borders

Answered By: Bryan Oakley

@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.

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)
Answered By: Xofo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.