Border for tkinter Label

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

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

Tags:

Python

Tkinter