Saturday, June 6, 2020

Easy Way to use Label in Python using Tkinter Example

Easy Way to use Label in Python using Tkinter Example


In Python, when you want to display your desired text or desired Image, then we use Tkinter. But first we understand about Tkinter.

What is Tkinter?

Tkinter is Python’s Basic Package or you can say it is Basic Graphical User Interface Toolkit, Which contains many widgets. And a beginner programmer can start GUI Base application using Tkinter.

So, Here SKOTechLearn Describe the easy way to use Label in Python using Tkinter Example. And we describe the use of Label widgets in python.

Check How if else statement will work in Python?


Now, Let’s start the basic Options, we need for Label Settings in Python.

Options Details and use
text
Input desire string or value for display

anchor  This will align the text in Left, Right, Center. eg.:
For Left align :  anchor=W
For Right align : anchor=E
For Center align : anchor=CENTER
by default Label already set in center align.

bg / background
Label’s background color change option

fg / foreground
Label’s Text or Font color change option

relief 
Option to set border style (raised, groove, solid, ridge, sunken, flat)

font Set the font with font name, size and style. This option syntax is:
font=("FontName", Size, style) eg: font=("Courier", 14, "bold")

image 
This option will be use to display image inside Label and Image Control.

textvariable 
This option is used to call predefined string to pass the variable in label to that particular class. This option uses StringVar for variable control.

bd / borderwidth
You can set border width from these two option: either bd or borderwidth.

height 
Increasing or decreasing Label’s height will be control by this option by passing value like: height=4

width
Increasing or decreasing Label’s width will be control by this option by passing value like: width=30


Now Lets start to understand how to pass variable in label and some basic label’s setting through above given options:

Here, first you have to import all widgets from Tkinter toolkit.
from Tkinter import *

After that you will proceed:
 from Tkinter import *
 Myroot = Tk()
 myLbl = Label( Myroot, text="SKOTechLearn", relief="groove", bd=2) 
 myLbl.pack()
 Myroot.title( "Tkinter Label in Python" )
 Myroot.mainloop()

After executing this code, the following output will be show:
Tkinter Label in Python

Tkinter window position and size setting with center screen process in Python


Now, Let’s see the textvariable example in Tkinter Label.

Font, background or foreground color and border settings of Label  in Python through Tkinter. 

 from Tkinter import *
 Myroot = Tk()
 myvar = StringVar()
 myLbl = Label( Myroot, textvariable=myvar, relief = "groove", font =( "Courier", 14, "bold"),  bd=1, fg = "red", bg ="#DDEEF0" )    
 myvar.set( "SKOTechLearn" )
 myLbl.pack()
 Myroot.title( "Label in Python" )
 Myroot.mainloop()

This will show the following output.
Font and color in Label in Python with Tkinter

And the Last Example will be use to show the multiple line label in Python.

Tkinter Multi Line Label in Python


So, the simple process is to use “\n” inside text variable like following example.
 from Tkinter import *
 Myroot = Tk()
 myvar = StringVar()
 myLbl = Label( Myroot, textvariable=myvar, relief = "groove", font =( "Courier", 14, "bold"),  bd=1, fg = "red", bg ="#DDEEF0" )   
 # Use "\n" for Next Line in Python by Following Way .
 myvar.set( "SKO \n Tech \n Learn \n Tips" )
 myLbl.pack()
 Myroot.title( "Label in Python" )
 Myroot.mainloop()

And the final Multiline Label in Python output will be as:
Multiline Label in Python using Tkinter
So, this is the easy way to understand how to use Tkinter label in Python with step by step process and descriptions described in SKOTechLearn Tips.

0 comments: