Python Kivy GUI Tutorial #23: Implementing an Image Viewer with FileChooserIconView and FileChooserListView

Posted by


In this tutorial, we will demonstrate how to create an image viewer using Kivy’s FileChooserIconView and FileChooserListView. The FileChooserIconView and FileChooserListView are widgets that allow users to select files from their local file system easily. We will use these widgets to allow users to select images from their system and display them in our application.

Let’s start by creating a new Python file and importing the necessary modules:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.filechooser import FileChooserIconView, FileChooserListView

Next, let’s create a class for our application and define the layout and functionality of our image viewer:

class ImageViewer(App):

    def build(self):
        layout = GridLayout(rows=2)

        # Create the file chooser icon view
        self.file_chooser_icon_view = FileChooserIconView()
        self.file_chooser_icon_view.filters = ["*.png", "*.jpg", "*.jpeg"] # Only show image files

        # Create the file chooser list view
        self.file_chooser_list_view = FileChooserListView()
        self.file_chooser_list_view.filters = ["*.png", "*.jpg", "*.jpeg"] # Only show image files

        # Add the file chooser widgets to the layout
        layout.add_widget(Label(text="Select an image:"))
        layout.add_widget(self.file_chooser_icon_view)
        layout.add_widget(self.file_chooser_list_view)

        return layout

    # Callback function when a file is selected
    def on_selection(self, selection):
        file_path = selection[0]
        image = Image(source=file_path)

        self.root.add_widget(image)

if __name__ == '__main__':
    ImageViewer().run()

In this code, we have created a class ImageViewer that subclasses App. In the build method, we have defined a GridLayout with 2 rows. We have created FileChooserIconView and FileChooserListView widgets and set their filters to only show image files. We have added these widgets to the layout.

Next, we have defined a callback function on_selection that will be called when a file is selected in the file chooser. We create an Image widget with the selected file path and add it to the root widget (the GridLayout).

To run the application, save the above code in a file named image_viewer.py and run it using the following command:

python image_viewer.py

You will see a window with the file chooser widgets displayed. You can select an image from your file system and it will be displayed in the application.

This is a simple example of how to create an image viewer using FileChooserIconView and FileChooserListView widgets in Kivy. You can customize the layout and functionality as needed for your application.

0 0 votes
Article Rating
34 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@marie-pierneault1035
28 days ago

Hey! Great video! I was wondering if it was possible to restrict the search to only some files, let's say only the files that are in the project. Can I get a hint!? Thanks 🙂

@rakshitkarkolli
28 days ago

i am unable get the output as you written code in video.

@kcseforecast7580
28 days ago

Do a pdf file viewer app using pdf files in project folders

@MVARUN-nv1in
28 days ago

here we access the data of the pc, but how to access the data of android?

@citoyenx1470
28 days ago

Please please Sir how to read pdf file in Kivy App

@citoyenx1470
28 days ago

Thanks for this video
Please please sir can you explain us how to read a pdf file in kivy app please

@citoyenx1470
28 days ago

Thanks Thanks Sir

@Murat_Bulut
28 days ago

Thanks a lot man you have been of great help. I love your style of simplifying subjects, keep up the good work !

@sobhandeepgope
28 days ago

how to use this in android?

@hbarudi
28 days ago

Interesting feature in kivy, but can I customize how the icons look in the kivy file explorer?

@osmarsouza4200
28 days ago

could you make a video explaining how to select and edit a csv file with the file picker?

@elderVelazquez73
28 days ago

Hello, Im new with kivy and I want to refresh the path, but I can't find a way to do that.
Whats the correct method to do that?

@McReaL87
28 days ago

Thank you so much!

@ak-up8fg
28 days ago

Can i explore pdf on kivy? Please make video about it.

@Teedies-oh7gi
28 days ago

I wanted to show the thumbnail of the video instead, is there a way to do that?

@leosayrip7306
28 days ago

Dear Sir can you tell me how to make screenshot with kivy or kivymd , it would be so helpful . thank you so much for the videos and tutorials.

@ziasilab8513
28 days ago

sir can we make an app from both mixed tkinter and kivy?

@adrianburton5144
28 days ago

Hey, everything runs perfectly but the app is just a blank screen even thought there are files in the directory

@saadahmedkhan3897
28 days ago

Why can't I do this on Android? It doesn't gives any errors!

@videosincoming6850
28 days ago

theres No File chooser in tkinter ??