Skip to content
Related Articles

Related Articles

How to Set Border of Tkinter Label Widget?
  • Last Updated : 11 Dec, 2020

The task here is to draft a python program using Tkinter module to set borders of a label widget. A Tkinter label Widget is an Area that displays text or images. We can update this text at any point in time. 

Approach

  • Import module
  • Create a window
  • Set a label widget with required attributes for border
  • Place this widget on the window created

Syntax: Label ( master, option, … )

Parameters:

  • Master: This represents the parent window.
  • Option: There are so many options for labels like bg, fg, font, bd, etc

Now to set the border of the label we need to add two options to the label property: 

  • borderwidth: It will represent the size of the border around the label. By default, borderwidth is 2 pixels. “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.

Given below is the implementation to set border and edit it as required.



Program 1: To set a border 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import tkinter
from tkinter import *
  
# Create Tk object
window = Tk()
  
# Set the window title
window.title('With_Border')
  
# set the window size
window.geometry('300x100')
  
# take one Label widget
label = Label(window, text="WELCOME TO GFG", borderwidth=1, relief="solid")
  
# place that label to window
label.grid(column=0, row=1, padx=100, pady=10)
window.mainloop()

chevron_right


Output:

Program 2: to set the border and edit it as required.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import tkinter
from tkinter import *
  
# Create Tk object
window = Tk()
  
# Set the window title
window.title('GFG')
  
# take Label widgets
A = Label(window, text="flat", width=10,
          height=2, borderwidth=3, relief="flat")
B = Label(window, text="solid", width=10,
          height=2, borderwidth=3, relief="solid")
C = Label(window, text="raised", width=10,
          height=2, borderwidth=3, relief="raised")
D = Label(window, text="sunken", width=10,
          height=2, borderwidth=3, relief="sunken")
E = Label(window, text="ridge", width=10,
          height=2, borderwidth=3, relief="ridge")
F = Label(window, text="groove", width=10,
          height=2, borderwidth=3, relief="groove")
  
# place that labels to window
A.grid(column=0, row=1, padx=100, pady=10)
B.grid(column=0, row=2, padx=100, pady=10)
C.grid(column=0, row=3, padx=100, pady=10)
D.grid(column=0, row=4, padx=100, pady=10)
E.grid(column=0, row=5, padx=100, pady=10)
F.grid(column=0, row=6, padx=100, pady=10)
  
window.mainloop()

chevron_right


Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :