Python Tkinter simple image viewer: Open and display images
Write a Python program to build a simple image viewer using Tkinter. The system allows the user to open and display image files.
Sample Solution:
Python Code:
Explanation:
In the exercise above -
- Import "tkinter" as "tk" and "filedialog" for creating the GUI components and opening the folder dialog. We also import PIL (Pillow) to work with images.
- Define the "open_image()" function, which opens the file dialog using "filedialog.askopenfilename()". This dialog allows the user to select an image file with specified file types (PNG, JPG, JPEG, GIF, BMP, ICO). If a file is selected, it calls the "display_image()" function.
- Define the "display_image()" function, which opens the selected image file using Pillow (Image.open()) and displays it in a Tkinter label widget. The ImageTk.PhotoImage class converts the image into a format that can be displayed in Tkinter. The status label shows the loaded image path.
- Create the main Tkinter window, set its title, and create an "Open Image" button that calls the open_image() function when clicked.
- Create a label widget (image_label) to display the loaded image, and a status label to show messages about image loading.
- The main event loop, root.mainloop(), starts the Tkinter application.
Sample Output:
Flowchart:

Python Code Editor:
Previous: Create and save text files.
Next: Python CSV file viewer with Tkinter.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.