How To Create Labels In Python TKinter

In this Python TKinter article i want to show you How To Create Labels In Python TKinter, so first of all let’s talk about Python TKinter.

 

 

What is Python TKinter ?

Python Tkinter is standard GUI (Graphical User Interface) library that comes with Python. it is builtin library and can be used to create desktop applications with graphical user interfaces.

Tkinter provides different widgets or graphical elements, including buttons, labels, text boxes, menus, canvas and many more, that you can use to create interactive GUIs. you can customize these widgets by changing their properties, such as size, color, font and style.

Tkinter is cross platform GUI toolkit, which means that the GUI applications you create with it can run on different operating systems, such as Windows, macOS and Linux. it is also easy to learn and use and this makes it popular choice for beginners who want to create simple desktop applications, TKinter is already packaged with Python, so you don’t need to install that.

 

 

 

This is the complete code for How To Create Labels In Python TKinter

 

 

So in the above code first we have imported all modules from TKinter, after that we have created our Root class you can check more information about this in Creating Window Article. so in the class we have added some requirements for the our main Window  like title, size and icon, also we have a method of createWidget() in this class. this method is just for creating of our label, so you can use Label in TKinter and we add some requirements for the Label like text, background color and foreground color also we have added a grid layout for our Label we will learn more about layout management in TKinter in further articles.

 

 

 

 

 

Run your code and this will be the output

How To Create Labels In Python TKinter
How To Create Labels In Python TKinter

 

 

 

 

 

So now let’s create another example on Python TKinter Label, in this example we will also use an image.

This example creates custom Tkinter widget called ExampleLabel that extends the tk.Frame class. widget contains label with an image on the left, label with text in the middle, and  button on the right. when the button is clicked, message is printed to the console.

To create the image label, the example loads an image file using the Pillow library (PIL) and resizes it to 200×200 pixels. it then creates PhotoImage object from the resized image and sets it as the image parameter of the tk.Label widget.

the text label is created using a standard tk.Label widget with custom font size of 20.

Finally, the button is created using a standard tk.Button widget with custom command function that prints a message to the console.

Note that the widget is first packed using the pack method and then the main event loop is started using the mainloop method of the root window.

 

 

 

Run the code and you will see this output.

How To Create Labels In Python TKinter
How To Create Labels In Python TKinter

 

 

 

 

Also you can read more articles on Python GUI Development

 

 

 

 

Also you can watch the complete video for this article.

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×