Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Border for tkinter Label

Tags:

python

tkinter

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!

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!

like image 597
Pax Vobiscum Avatar asked Sep 09 '16 16:09

Pax Vobiscum


People also ask

How do you put a border around a label?

Select the finished label document; click on the Border button drop down arrow, and select the option for "All Borders." If you want a customized border for the labels, select the label document and go to the Borders and Shading dialog box, under Settings choose All, and then choose a different Style, Color or Width; ...

Which widget to choose if you want a border and a label on a frame?

“bd” can also be used as a shorthand for borderwidth. relief: It will Specify the look of a decorative border around the label. By default, it is FLAT. Other than Flat there are many more acceptable values like raised, ridge, solid, etc.


2 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

like image 96
Bryan Oakley Avatar answered Sep 25 '22 16:09

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) 
like image 32
Xofo Avatar answered Sep 25 '22 16:09

Xofo