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.
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 🙂
i am unable get the output as you written code in video.
Do a pdf file viewer app using pdf files in project folders
here we access the data of the pc, but how to access the data of android?
Please please Sir how to read pdf file in Kivy App
Thanks for this video
Please please sir can you explain us how to read a pdf file in kivy app please
Thanks Thanks Sir
Thanks a lot man you have been of great help. I love your style of simplifying subjects, keep up the good work !
how to use this in android?
Interesting feature in kivy, but can I customize how the icons look in the kivy file explorer?
could you make a video explaining how to select and edit a csv file with the file picker?
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?
Thank you so much!
Can i explore pdf on kivy? Please make video about it.
I wanted to show the thumbnail of the video instead, is there a way to do that?
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.
sir can we make an app from both mixed tkinter and kivy?
Hey, everything runs perfectly but the app is just a blank screen even thought there are files in the directory
Why can't I do this on Android? It doesn't gives any errors!
theres No File chooser in tkinter ??