Hello Friends, In the last tutorials I explained about Tkinter introduction and how to use labels in Tkinter.If you haven’t read then you can read here:
- Also Read : Python GUI Tkinter Tutorial Introduction
- Also Read : Python Tkinter Label Tutorial and Example
Please read the above articles to clearly understand this tutorial.
How To Use Images In Python Tkinter Labels
First, let me write some code and I will explain you every step.
Just run this script and my output is as follows:
I have already explained the first two lines of the code in the last tutorials please read them.Let me start fromthe third line.
imagePath = PhotoImage(file="hacker.gif")
PhotoImage method typically has one parameter is called file which is nothing but a path of that certain image.
As of Now Python Tkinter supports some image formats only like GIF and PPM/PGM.
I have downloaded the above GIF image from Google Images 😛
widgetf = Label(main, image=imagePath).pack(side="right")
Now we are going to create a widgetf variable is a variable of Label method that has two parameters one for parent window (main) and another is Image parameter for image path.I have packed this image on the right side of the parent window (main) using side parameter that has typically four values(right,left,top,bottom).you can check it to know how this works simply by changing its value
comments = """Hakcers are here....Don't Underestimate Us. we build things and we break it"""
This is just a multi-line text that is going to be used in another label later.(“””) these are used to write multiple lines of text in the python.
widgets = Label(main, justify=LEFT, padx = 10, text=comments).pack(side="left")
This is the second widget for the main (parent window) variable.In the above script we have four parameters first one is assigned to the parent window,justify is used for position or alignment of text in that label, If you have been to HTML then you might know how padding works.Here fourth one is text parameter that expects some text string as its value.We have already created the text string (comments variable) and assigned to it.For the image widget,we packed it to right.So I’m packing this widget to right.
If you want the text appears on the image then just add compound parameter to the second widget or simply run this code and analyze
And I have already explained how mainloop() method works on previous articles.Just go through them to get the idea.
Please, Like Our Facebook Page For More Updates and Comment Here.