Displaying Images in tkinter

Displaying Images in tkinter

Hello guys, as usual we are sharing one more source code using which you can display images inside your GUI screen of tkinter

We will restrict ourselves to .png format only. For .jpg format, we have given an example as comments

Tkinter GUI is a python library which is designed in such a way that it’s use is very easy and also there you will take very less time to learn it

In this code, we have imported full tkinter using

from tkinter import *

Then we have created a reference to create the root to the screen using

root = Tk()

Now let’s set the screen size using

root.geometry("500x500")

Note: Here ‘x‘ is small case letter

After setting size, we are loading image using PhotoImage, then assigning an image to the label and finally packing it

photo = PhotoImage(file="imagename.png")
myimage = Label(image=photo)
myimage.pack()


Then we set a simple text using Label widget and pack it as well

text = Label(text="this is my gui")                                                                                           text.pack()

Now, to call the GUI screen we need to type

root.mainloop()

Now let’s have a look at the full source code.


Full Code


from tkinter import *
root = Tk()
# width x height
root.geometry("550x550")
photo = PhotoImage(file="imagename.png")
myimage = Label(image=photo)
myimage.pack()
# for jpg format
# image = Image.open("imagename.jpg")
# photo = ImageTk.PhotoImage(image)
# myimage = Label(image=photo)
# myimage.pack()
text = Label(text="this is my gui")
text.pack()
root.mainloop()

Output



Thanks for reading


Keep updated with us for more amazing free source codes like this.


Also read:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you