Open In App

How to Set Border of Tkinter Label Widget?

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

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: 



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

Program 1: To set a border 




# 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()

Output:

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




# 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()

Output:


Article Tags :