Search code examples
pythonkivykivy-languagefilechooser

How to Make Kivy File Chooser auto refresh in python


So I have been coding this file explorer program. I have coded the copy function so far and the problem I am running into is that after I copy a file to its new directory(i.e. the directory that I am on currently in the filechooser) the file isn't showing up in the the actual filechooser. I am using the icon view and I saw only one mention of this online here:Refresh / Reload FileChooser and I tried this method and like the actual _update_files() function is getting executed.(checked it with a couple of print statements) But I am noticing that there is not any change in the actual filechooser.What am i doing wrong here? THANK YOU!!!

This is the python code that is executed when ever the file is copied. I assigned the actual screen that contains the file chooser to a variable named MainScreenVar

        for i in range(0, self.execute_data_length):

            if self.execute_data[i][2] == "File" :
                shutil.copy2(self.execute_data[i][1], current_path)
                MainScreenvar = MainScreen()
                return MainScreenvar.ids.filechooser._update_files()
        mycursor.execute("DELETE FROM selection")
        mydb.commit()

This is part of the kivy file:

<MainScreen>:
FileChooserIconView:
    id:filechooser
    size_hint:1,.9
    pos_hint:{"top": .9}
    color: 0,0,0,0

and the source code for the kivy file chooser is here: https://kivy.org/doc/stable/_modules/kivy/uix/filechooser.html


Solution

  • The error in your code is the line:

    MainScreenvar = MainScreen()
    

    which is creating a new instance of MainScreen. This will not be the instance that is displayed in your GUI, so the line:

    return MainScreenvar.ids.filechooser._update_files()
    

    is calling _update_files() for a FileChooserIconView that is not displayed in your GUI. The solution is to use a reference to the actual FileChooserIconView that is in your GUI. Probably something like:

    MainScreenvar = self.manager.get_screen('main')
    

    However, this is only a guess since you haven't provided much of your code. This assumes that your posted code is from a Screen, and that the name that you provided for MainScreen is main.