Python Tkinter Label Tutorial And Example

Python Tkinter Label Introduction

As I said in the earlier article Python Tkinter Tutorials Introductionwe have many widgets are available in the Tkinter module and among that Label is the most functional,helpful and key  widget.Simply a label is the widget class of Tkinter.

Also Read: How To Chat With Your Friend Using Command Prompt

How To Write A String Using Tkinter Labels

Let me write a simple text using Tk labels in python programming and then I will explain you.

from Tkinter import *
#import everything from Tkinter module

main = Tk()

t = Label(main, text="Hello World!")
t.pack()

main.mainloop()

If you run this script successfully,you will get something like this :

Also Read :   Python GUI Tkinter Tutorials Introduction | Examples

untitled

Okay, Let me explain this how it works step by step:

from Tkinter import *

The first step is to import Everything from Tkinter module of python.

Note: I am using 2.X.X version of python.For 3.x.x the module name has been changed. Just write ” tkinter ” (without Quotes) instead Tkinter. 🙂

 

from tkinter import * #For 3.x.x Versions

Also Read : How To Use Python on Command Prompt

main = Tk()

In the next step, I have created a variable called main assigned it to TK().For every Tk project first, we need to create this main to access the different widget classes inside the program.Here main is the parent window of the program.Whatever you wrote inside it,that will be displayed in that window only.So you should remember that this is mandatory for every program.

t = Label(main, text="Hello World!")

In the next line, we have to create a Label which typically has two or more parameters the first one is always for its root variable (main) and another parameter is used for different purposes.At present, we are using text parameter to write our text string and I have assigned the whole label to a variable called t.

t.pack()

Simply t.pack() method helps to fit the size of t Label to the root window.

Also Read :   How To Use Or Customize Fonts In Python Tkinter Labels

Also Read : Python GUI Tkinter Tutorial Introduction

main.mainloop()

If you want your Tk will remain open until you close it then you should use the mainloop() method.This will keep looping the Tk window until you close it.

Hope It will work for you.Read all the articles in this category to understand Tk.

Please, Like Our Facebook Page For More Updates 

Leave a Reply

Your email address will not be published. Required fields are marked *