Label, Text, Entry & Message Widget in Python

Label, Text, Entry and Message Widget in Python

Label

Labels are used to display texts and images.
The label can only display text in a single font, but the text may span more than one line.
Syntax
Here is the simple syntax to create this widget −

w = Label ( master, option=value, ... )

Parameters
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.
Various options of Label widget:

  • anchor: This option controls where the text is positioned. The default anchor is CENTER.Other possible values are NW(North West is top left), N, NE(North East is top right), W, E, SW(South West is bottom left), S, SE(South East is bottom right)
  • bg : The normal background color displayed behind the label and indicator.
  • bd : The size of the border around the indicator. Default is 2 pixels.
  • cursor : If you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that pattern when it is over the label.
  • bitmap : Set this option equal to a bitmap or image object and the label will display that graphic.
  • font : If you are displaying text in this label (with the text or textvariable option, the font option specifies in what font that text will be displayed.
  • fg : If you are displaying text or a bitmap in this label, this option specifies the color of the text. If you are displaying a bitmap, this is the color that will appear at the position of the 1-bits in the bitmap.
  • height : The vertical dimension of the new frame.
  • image : To display a static image in the label widget, set this option to an image object.
  • text : To display one or more lines of text in a label widget, set this option to a string containing the text. Internal newlines (“n”) will force a line break.
  • textvariable : To slave the text displayed in a label widget to a control variable of class StringVar, set this option to that variable.
  • underline : You can display an underline (_) below the nth letter of the text, counting from 0, by setting this option to n. The default is underline=-1, which means no underlining.
  • width: Width of the label in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
  • relief : Specifies the appearance of a decorative border around the label. The default is FLAT; for other values.

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date :18th March 2020

Description : Program to show use of Label widget

"""
from tkinter import *

root = Tk()
var = StringVar()
label = Label( root, textvariable=var, relief=RAISED )

var.set("Welcome to itvoyagers.in")
label.pack()

OUTPUT

Output for Label widget

Text

The Text widget is used to display the multi-line formatted text with various styles and attributes. The Text widget is mostly used to provide the text editor to the user.
A text widget manages a multi-line text area.
The Text widget also facilitates us to use the marks and tabs to locate the specific sections of the Text.
We can also use the windows and images with the Text as it can also be used to display the formatted text.

Syntax
Here is the simple syntax to create this widget −

w = Text( master, option=value, ... )

Parameters
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.
Various options of Text widget:

  • bg : The normal background color displayed.
  • bd : The size of the border around the indicator. Default is 2 pixels.
  • font :The font type of the text.
  • fg : The text color of the widget.
  • height : The vertical dimension of widget lines.
  • state : It the state is set to DISABLED, the widget becomes unresponsive to the mouse and keyboard unresponsive.
  • width: Width of the text widget in characters. If this option is not set, the text will be sized to fit its contents.
  • highlightbackground : The highlightcolor when the widget doesn’t has the focus.
  • highlightthickness : The thickness of the focus highlight. The default value is 1.
  • highlighcolor : The color of the focus highlight when the widget has the focus.
  • relief : Specifies the appearance of a decorative border around the text widget. The default is FLAT; for other values.

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date :18th March 2020

Description : Program to show use of Text widget

"""

from tkinter import *

root = Tk()
txt = Text( root, fg = 'orange' ,bd=10,bg="black" ,relief=RAISED )
txt.insert(INSERT,"Welcome to itvoyagers.in n")
txt.insert(INSERT,"This code is for Text Widget n")
txt.insert(INSERT,"Enjoy learning with itvoyagers n")
txt.pack()


OUTPUT

Output of text widget

Entry

The Entry widget is a standard Tkinter widget used to enter or display a single line of text.
The entry widget is used to enter text strings. This widget allows the user to enter one line of text, in a single font.
To enter multiple lines of text, use the Text widget.

Syntax
Here is the simple syntax to create this widget −

w = Entry( master, option=value, ... )

Parameters
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.
Various options of Entry widget:

  • bg : The normal background color displayed.
  • bd : The size of the border around the indicator. Default is 2 pixels.
  • font :The font type of the text.
  • fg : The text color of the widget.
  • height : The vertical dimension of widget lines.
  • state : The default is state=NORMAL, but you can use state=DISABLED to gray out the control and make it unresponsive. If the cursor is currently over the checkbutton, the state is ACTIVE.
  • show : Normally, the characters that the user types appear in the entry. To make a .password. entry that echoes each character as an asterisk, set show=”*”.
  • justify : If the text contains multiple lines, this option controls how the text is justified: CENTER, LEFT, or RIGHT.
  • textvariable : To slave the text displayed in a entry widget to a control variable of class StringVar, set this option to that variable.
  • width: Width of the entry widget in characters. If this option is not set, the entry will be sized to fit its contents.
  • highlightbackground : The highlightcolor when the widget doesn’t has the focus.
  • highlightthickness : The thickness of the focus highlight. The default value is 1.
  • highlighcolor : The color of the focus highlight when the widget has the focus.
  • relief : Specifies the appearance of a decorative border around the text widget. The default is FLAT; for other values.
  • cursor : If you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that pattern when it is over the checkbutton.

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date :18th March 2020

Description : Program to show use of Entry widget

"""

from tkinter import *
root=Tk()
var1=StringVar()
var2=StringVar()
itv1=Label(root,text="Username").grid(row=1,column=0)
itv2=Entry(root,textvariable=var1,bd=10, fg='orange',bg = 'black', font = 'Helvetica').grid(row=1,column=1)
itv1=Label(root,text="Password").grid(row=2,column=0)
itv2=Entry(root,show="*",textvariable=var2,bd=10, fg='orange',bg = 'black').grid(row=2,column=1)
def submit():
    print("welcome",var1.get())
    print("your password is",var2.get())
b=Button(root,text="Submit",command=submit).grid()


OUTPUT

Output for Entry widget

Message

The message widget shows the text messages to the user which can not be edited.
This widget provides a multiline text, automatically breaking lines and justifying their contents.

Its functionality is very similar to the one provided by the Label widget, except that it can also automatically wrap the text, maintaining a given width or aspect ratio

The message text contains more than one line. However, the message can only be shown in the single font.

Syntax
Here is the simple syntax to create this widget −

w = Message( master, option=value, ... )

Parameters
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.
Various options of Message widget:

  • anchor: This option controls where the text is positioned. The default anchor is CENTER.Other possible values are NW(North West is top left), N, NE(North East is top right), W, E, SW(South West is bottom left), S, SE(South East is bottom right)
  • bg : The normal background color displayed behind the Message widget.
  • bd : The size of the border around the indicator. Default is 2 pixels.
  • font : If you are displaying text in this widget (with the text or textvariable option, the font option specifies in what font that text will be displayed.
  • fg : If you are displaying text or a bitmap in this label, this option specifies the color of the text. If you are displaying a bitmap, this is the color that will appear at the position of the 1-bits in the bitmap.
  • height : The vertical dimension of the new frame.
  • image : To display a static image in the widget, set this option to an image object.
  • text : To display one or more lines of text in a message widget, set this option to a string containing the text. Internal newlines (“n”) will force a line break.
  • textvariable : To slave the text displayed in a message widget to a control variable of class StringVar, set this option to that variable.
  • width: Width of the label in characters (not pixels!). If this option is not set, the widget will be sized to fit its contents.
  • relief : Specifies the appearance of a decorative border around the widget. The default is FLAT; for other values.

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date :18th March 2020

Description : Program to show use of Message widget

"""

import tkinter
from tkinter import *
root=Tk()
var=StringVar()
itv1=Message(root,textvariable=var,relief=RAISED)
var.set("Welcome to itvoyagers.in")
itv1.pack()
def helloCallBack():
    tkinter.messagebox.showinfo("I am title","Enjoy Learning with itvoyagers")
B=tkinter.Button(root,text="Click Me",command=helloCallBack)
B.pack()



OUTPUT

Output of Message widget

NOTE

LabelMessageEntryText
It is non-editable widgetIt is non-editable widgetIt is editable widgetIt is editable widget
It is single line which can span to multi-lineIt is multi-line widgetIt is single line widgetIt is multi-line widget
Displays textDisplays text messageCan display text to user and accept text from userCan display text to user and accept text from user

You can also check following posts to know more about tkinter and GUI

Leave a Comment