Python tkinter label – How to use

Are you trying to learn Python GUI programming? Let us check out in detail the Python Tkinter label. How to use Label in Python Tkinter. We will cover all the topics as :

  • About Tkinter label
  • Tkinter label font size
  • Tkinter label position
  • Tkinter label border
  • Tkinter label transparent background
  • Tkinter label attributes
  • Tkinter label image
  • Tkinter label attributes
  • Tkinter label image
  • Tkinter label text-align left

Python Tkinter label

Let us see what is a Python Tkinter label?

  • The label simply means the text on the screen.
  • It could be an instruction or information.
  • Labels are the widely used widget & is a command in all the GUI supporting tools & languages.
  • Labels are also used to display images & icons.
  • Few popular label options are:
    • text: to display text
    • textvariable: specifies name who will replace text. Mostly used when text is changing.
    • underline: underlines just one character of the text string
    • width: adds addition width to label, takes a number as an argument.
    • image: to add an image or to style the label. It uses style.map()
    • compound: display images with text, also provide control to position them.

Also, you may like, Python Tkinter Title.

Tkinter label font size

Let us see how to set font size in Python Tkinter label.

  • Font-size creates emphasis on user.
  • It makes things more clear & readable.
  • In label font size can be controlled using keyword font

Syntax:

In this syntax ws is the master that we are using, font-name could be any font supported by ms word like Arial, Times new roman, Calibri, etc. weight can be Bold, italic, underline.

Label(ws, text="any text here", font=('font-name & weight', 'font-size')

Code:

from tkinter import *

ws = Tk()

Label(ws, text="Hello there!", font=("arial italic", 18) ).pack()

ws.mainloop()

Output:

So in this output, you can see that the text “Hello there!” have size 18 and is italic. Similarly, you can put bold and underline.

Python tkinter label
Python tkinter label

You may like, BMI Calculator Using Python Tkinter.

Tkinter label position

  • The right placement of widgets can create a difference.
  • Label position can be controlled using pack, grid & place
  • refer to our geometry positioning section to know more about them
READ:  ValueError: setting an array element with a sequence error in Python [4 methods to fix]

Syntax:

Label(ws, text="any text", font=(14, "roboto")).pack()
Label(ws, text="any text", font=(14, "roboto")).grid(row=value, columns=value)
Label(ws, text="any text", font=(14, "roboto")).place(x=coordinate_value, y=coordinate_value)

Tkinter label border

  • The Python Tkinter Label border defines the type of border & its thickness.
  • There 6 types of borders each having their on property:
    • Flat
    • Raised
    • Sunken
    • ridge
    • solid
    • groove
  • By default flat is active.
  • borderwidth keyword is used to define the thickness of the border.
  • relief keyword is used to define the type of border.

Syntax: borderwidth should be provided any integer value, relief should provide anyone out of these ( flat, raised, sunken, ridge, solid, groove).

Label(ws, text="any text", borderwidth=value, relief="typeofborder").pack()

Code:

from tkinter import *

ws = Tk()
ws.title("Border")
ws.geometry("300x400")

Label(ws, text="Flat border", borderwidth=3, relief="flat", padx=5, pady=10).pack(padx=5, pady=10)
Label(ws, text="raised border", borderwidth=3, relief="raised", padx=5, pady=10).pack(padx=5, pady=10)
Label(ws, text="sunken border", borderwidth=3, relief="sunken", padx=5, pady=10).pack(padx=5, pady=10)
Label(ws, text="ridge border", borderwidth=3, relief="ridge", padx=5, pady=10).pack(padx=5, pady=10)
Label(ws, text="solid border", borderwidth=3, relief="solid", padx=5, pady=10).pack(padx=5, pady=10)
Label(ws, text="groove border", borderwidth=3, relief="groove",padx=5, pady=10).pack(padx=5, pady=10)

ws.mainloop()

Output:

In this output, all types of border are displayed, each have width of 3 pixels. padx & pady determines the extra space around the box.

Python Label border width
Tkinter label border

Tkinter label transparent background

  • Transparent background means you can see through the frame.
  • only an image will appear the rest frame will be invisible.
  • make sure to take an image that does not have the background
  • to provide bg color to the image and assign the same color to wm_attributes command.
  • we tried in all colors but grey works best.

Syntax:

This colourname should be same as the image bg color. We tried with many colors but grey works best for this.

ws.wm_attributes("-transparentcolor", 'colorname')

Code:

from tkinter import *

ws = Tk()

giant=PhotoImage(file='bg.png')
Label(ws,image=giant,bg='grey').pack()
ws.wm_attributes("-transparentcolor", 'grey')

ws.mainloop()

Output:

In this output, picture of giant has been displayed with no background. You can see through the image frame.

Python Label transparent background
https://static.wikia.nocookie.net/clashofclans/images/3/35/Giant_info.png/revision/latest/scale-to-width-down/340?cb=20170927232347

Tkinter label attributes

  • Attributes refer to features of the label
  • They are also called options that facilitate more functions
  • though we have discussed most of them in this blog.
  • but we are summarising all of them here.
  • Attributes are as following:
  1. colours:
  • colours plays an important role in software development.
  • Every colours has some meaning like red for error, green of correct, etc.
  • bg is used to fill background colour of the label
  • fg is used to change the text colour.
  • you can either provide a colour name or hex code

Example:

Label(ws, text="colors", bg="blue", fg="#000").pack()

2. font :

  • fonts make text readable.
  • to know more about fonts
  • Please refer to our Tkinter label font size section
READ:  How to create web form in Python Django

Example:

Label(ws, text="font demo", font=('arial bold', 18)).pack()

3. relief:

  • relief is used to provide decoration to the border.
  • It has various options that can be used to emphasise text.
  • To know more about options check Tkinter label border section.

Example:

Label(ws, text="relief demo", borderwidth=3, relief='solid border').pack()

4. cursor :

  • It used to change the cursor image when hovered over the label.
  • here is the list of all cursors that one can use
  • arrow, circle, clock, dotbox, exchange, fleur, heart man, mouse, pirate, pls, shuttle, sizing, spider, spraycan, star, target, tcross, trek, watch

Example:

Label(ws, text="cursor demo", cursor="watch").pack()

5. Height & Width:

  • Height determines the vertical dimension of the label.
  • width determines the horizontal dimension of the label

Example:

Label(ws, text="Height & width", height=5, width=10).pack()

6. padx & pady

  • padx adds empty space vertically
  • pady adds empty space horizontally
  • if they are used with in the label then they add extra space inside the box
  • if they are used in the positioning section (pack/grid/place) then add space outside the box.

Example: Inside the box

Label(ws, text="padding demo", padx=10, pady=5).pack()

7. Justify:

  • Justify is used for alignment.
  • It works similar to anchor but has only three options
  • LEFT, CENTER, RIGHT
  • programmers prefer to use the anchor as it gives more control

Example:

Label(ws, text="padding demo", justify=CENTER).pack()

8. anchor:

  • Anchor also provides controls for alignment.
  • to know more about it refer to our section Tkinter label text-alignment

Example:

Label(ws, text="padding demo").pack(anchor='w')

So these were popular attributes for Label. Most of them work in other widgets as well as entry, button, etc. Now before we end this section, here is the program with attributes used in it. The controls are placed in a sequence for your convince. Do notice each and every attribute used. cursor one is my favorite

Code:

from tkinter import *

ws = Tk()
ws.title("pythonguides")
ws.geometry('200x150')

Label(ws, text='danger', bg='red', fg='red', font=('Arial bold', 14), relief='raised', cursor='pirate', height=4, width=10, padx=10, pady=10, justify=CENTER).pack(padx=10, pady=10, anchor='w')

ws.mainloop()

Output:

This is a simple program created using all the attributes mentioned above. Few highlights, the cursor has transforming ability to the pirate icon, though it is pack() still it is in the corner because of anchor set to west, blue is bg & red text is fg. Text is justified to center, etc.

Python Label attributes
Tkinter label attributes

Tkinter label image

  • Images make things interesting.
  • In this section we will learn how to put image in label
  • We will also learn to place text on image.
READ:  Matplotlib invert y axis

Syntax:

var-name = PhotoImage(file="path/of/image.extension")
Label(ws, image=var-name).pack()

Code:

from tkinter import *

ws = Tk()
ws.title("giant image")

giant = PhotoImage(file="bg.png")
Label(ws, text="hi",image=giant).pack(side="top")
intro = '''Hi! I am Giant from COC, I have been programmed to
break walls & bones.''' 
Label(ws, text=intro, anchor=S, font=('Arial bold', 14), underline=9).pack(side="bottom")

ws.mainloop()

Output:

In this output, giant image has been placed, below the image there is a brief information about him. In the intro section G is underlined and for that we have used underline option in label.

Tkinter label image
Tkinter label image

Tkinter label text-alignment

  • Alignment refers to the quick position of text direction wise.
  • direction-wise means:
    • N : North
    • S : South
    • E : East
    • W : West
    • NE : North East
    • NW : North West
    • SE : South East
    • SW : South West
    • CENTER
  • anchor keyword is used to position the label widget.

Syntax: In place of direction provide any one out of these (SN, SE, W, NE, SE, SW, CENTER)

Label(ws, text="sometext", anchor= direction).pack()

Code:

from tkinter import *

ws = Tk()
ws.title("Border")
ws.geometry("50x100")

Label(ws, text="Left", anchor=W).pack(fill='both')
Label(ws, text="Right", anchor=E).pack(fill='both')

ws.mainloop()


Output:

In this output, you can see that text has been aligned to left and right using anchor.

Python Label text-alignment
Python Label text-alignment

You may like the following Python tutorials:

In this tutorial we have learned Python tkinter label.

  • What is the Tkinter label in Python
  • Tkinter label font size
  • Tkinter label position
  • Tkinter label border
  • Tkinter label transparent background
  • Tkinter label attributes
  • Tkinter label image
  • Tkinter label attributes
  • Tkinter label image
  • Tkinter label text-align left