Coders Packet

Image Viewer in Python using Tkinter

By Tanmoyee Roy

This is an application which helps us to view images stored in the program using Tkinter and pillow.

For building this image viewer application at first we will be requiring two libraries: Tkinter and Pillow. So we need to install them first. Now Tkinter is by default available with python. So there's no need to install it manually. To install the pillow go to the command prompt/command line and type: pip install pillow

Now in the code editor, we need to make a file with a .py extension, in our case it's Image_Viewer_Application.py. Then download/place the images that we want to view using this application in the same directory as the .py file. Finally, we are ready to start our code.

We will import both the Tkinter and the pillow libraries because Tkinter is needed for the GUI part and PIL will be required to add the images as it is the imaging library. From the Tkinter library, we will be importing the whole library while from PIL we will be importing ImageTk and Image library

After this, we will be initializing the application using root and provide the application a name using root. title().

And after that, we need to load our images. In our case, it's four images. Then we will be storing those images in a dictionary and then create a variable that holds the image.

To launch our application, we need to write:

Now we will be creating the three buttons for our GUI namely the back_button, the forward_button, and the exit_button. The backward and forward are the functions. The command is a kind of adding event listener i.e when we click any button, the corresponding function will be called.

So now, we need to make the grid to view our images. And then add our three buttons also.

Now, all we are left with is to define the functions forward and backward. Let's define the forward function at first.

The backward function is almost the same with only a change in the if statement.

Thus by this, we can successfully build our image viewer using python programming. There is a drawback i.e we can view only PNG files because Tkinter does not support any other file format yet.

The output is shown below:

 

 

Download Complete Code

Comments

No comments yet