Sunday, October 1, 2023
HomejQuery6 Examples to Be taught Python tkinter label

6 Examples to Be taught Python tkinter label


What’s label widget in tkinter

The tkinter is a Python library for creating GUI-based purposes.

  • The label is among the obtainable widgets in tkinter like buttons, entry, and so forth.
  • Label widget is a field the place you could place textual content or picture.
  • Typically, that is used as an data field explaining the aim of one other widget.
  • For instance, we’ve an entry widget (single-line textbox) within the type to enter title.
  • To tell the customers goal of that entry widget, you could use a label beside the textbox.

The part beneath reveals utilizing the label widget with numerous obtainable choices.

A easy label instance

On this instance, we created a window and positioned a label in it. No different widget is created. Take a look:

Code:



Output:

tkinter-label-simple

Creating labels with background and foreground colours

By utilizing bg choice, you could create a label with a background colour. Equally, the textual content colour of the label will be set by the foreground colour choice i.e. fg.

See a program with a inexperienced background and white foreground colour label beneath:



Output:

tkinter-label-fg-bg

A label with a textbox/entry instance

Within the instance beneath, we create a label together with a textbox/entry widget to indicate the aim of the textbox.

Code:



Output:

tkinter-label-entry

Utilizing a picture in label instance

Relatively than textual content, you may additionally use picture within the label. For that, use the picture choice. First, take a look at an instance beneath after which we are going to clarify the way it labored.

Code:



Output:

tkinter-label-image

How did it work?

Step 1:

Contemplate this line:

from PIL import Picture, ImageTk

We used Pillow Picture Library to learn and convert photographs to Python objects, so we are able to show them.

With a purpose to set up PIL, execute this command within the CMD (when you don’t have already got):

pip set up pillow

 Step 2:

Then we specified the picture on this line:

img = ImageTk.PhotoImage(Picture.open(“E:/emblem.png”))

Set the trail in response to your system.

Step 3:

Utilizing picture choice within the label:

lbl_img = tk.Label(win, picture = img)

An instance of padding choices

Chances are you’ll add further area to the left, proper, high, and backside of the textual content through the use of the padding choices of the label widget.

For including padding to the left and proper of the textual content, use padx choice.

So as to add area within the high and backside, use the pady choice.

Code:



Output:

tkinter-label-paddings

Altering the font dimension and title of the label instance

Within the instance beneath, we created various-sized labels through the use of the font choice of the label widget. Apart from, font names are additionally given for 2 labels Verdana and Arial.

Code:



Output:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments