Python Forum
Kivy Filechooser, Saving Selection
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Kivy Filechooser, Saving Selection

I'm working on an app where (among other things)  a user can choose a folder as a working directory. i.e.  A user chooses a folder the first time he uses the app, and then the folder path will be saved within the app when he re-opens it.

I've attached code for a simplified version of the app. Overview of how it works:

1) If you click "Show Current Folder", the app will read from the file "" and pull in the text of a variable called userPath. For instance, it will say, "The current folder is C:\xyz".
2) If you choose a folder in the filechooser and then click "Change Folder", the path in (variable "userPath") will be changed to the new path. 
3) If you click "Show Current Folder" again, it should show the new path.

Problem: For #3, the popup shows the old path. In order to show the new path, I have to close and re-open the app. I've added reload(destination) statements within my code, but it doesn't seem to have any effect.

Any ideas on how I can get the "Show Current Folder" function to show the newly created path?

Thanks. I really appreciate any and all help/advice.

(Note for code below: The main code will work immediately--you just have to save the "' code in the app folder. If there is a more effective way of doing this within a single app file, I'd love to know. Apologies for any inconvenience.) code:

userPath: '' code:

from kivy.config import Config

Config.set('graphics', 'width', '700') 
Config.set('graphics', 'height', '700')
Config.set('graphics', 'fullscreen', 'fake')

from import App
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.listview import ListView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from import ObjectProperty, StringProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.core.text import LabelBase
from kivy.utils import get_color_from_hex as C
from kivy.uix.switch import Switch
from kivy.uix.checkbox import CheckBox
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
from os.path import join, isdir
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.modalview import ModalView
import destination

import pandas as pd
from pandas import *
import numpy as np
import datetime
from datetime import *
import win32com.client
import re
import os

from import ListProperty

# Create both screens. Please note the root.manager.current: this is how
# you can control the ScreenManager from kv. Each screen has by default a
# property manager that gives you the instance of the ScreenManager used.
#:import C kivy.utils.get_color_from_hex
#:import WipeTransition kivy.uix.screenmanager.WipeTransition


#color: .5,.6,.2,.7

        spacing: 1

            #background_color: C('#bfbf40')
            text: "Back to Menu"
            on_release: app.root.current = 'main'

            background_color: C('#bfbf40')
            text: "Change Folder"
            on_release: root.set_folder()

            background_color: C('#bfbf40')
            text: "Show Current Folder"
            on_release: root.popup_current() 

            orientation: "vertical"
                size_hint: 1,.8
                id: filechooser
                filters: [root.is_dir]
                font_name: 'Roboto-Bold'
                rootpath: "C:\Users"
                dirselect: True
                #on_selection: my_widget.selected(filechooser.selection)
                on_selection: root.show_selection()

                size_hint: 1, .1
                text: root.new_path

<[email protected]>:
    cols: 2
    spacing: 10
        (0.5* (self.width - self.spacing[0]) - self.padding[0])
    row_force_default: True
#col_default_height: 50
#(1*(self.height - self.spacing[0]) - self.padding[0])
#col_force_default: False

<[email protected]>:
    background_normal: 'button_normal.png'
    background_down: 'button_down.png'
    background_color: C('#95A5A6')
    font_size: 24
    #halign: 'center'
    markup: True

    background_normal: 'button_normal.png'
    background_down: 'button_down.png'
    background_color: C('#95A5A6')
    font_size: 24
    markup: True
    halign: 'center'


    size_hint: .8, .4
    title: "Current Folder"
        orientation: 'vertical'

                id: content_text
                size_hint_y: None
                height: self.texture_size[1]
                #text: "Current folder is: " + root.msg
                text_size: (self.width-20), None
                line_height: 1.5
                valign: "top"
                text: 'Current folder is: ' + root.msg

            text: "Proceed"
            size_hint_y: None
            height: "40dp"
            on_press: root.dismiss()


# Declare both screens

class FileScreen(Screen):
    current_path = StringProperty()
    new_path = StringProperty()
    msg = StringProperty()
    def popup_current(self):
    def show_selection(self):
        sel= self.ids["filechooser"]
            self.new_path = sel.selection[0]
            print self.new_path
            return self.new_path

    def is_dir(self, directory, filename):
        return isdir(join(directory, filename))

    def set_folder(self):
        f= open('','w')
        p="userPath='"+ directory+"'"
        print p

class PopupCurrent(Popup):
    msg= StringProperty()
    msg = destination.userPath

class TestApp(App):
    def build(self):

        # Create the screen manager
        sm = ScreenManager()


        return sm
    def handledrops(self, *args):
        for i in self.drops:

if __name__ == '__main__':

This is looks like totally wrong approach. Configurations like this should be stored in the config file and kivy has nice interface to work with config files. All the config settings are loaded in the memory and you should work with these. If user changes configuretaion - you save to the config file, in order to be available for next time when user starts the program, but still you work with config settings that are loaded (as been changed in the meantime, i.e. in memory is always copy of the config settings as in teh file).
Even if there was no interface for config files in kivy, this should be a variable (most probably app attribute) that you work with, instead of reloading py module.
Something else I should have mentioned - the module which you import will be compiled if in the future you compile your kivy app to apk (Android) or exe (Win) , so you will not be able to do what you are doing now in your code.
Thanks! I looked into your suggestion and found that I could leverage Kivy's Settings/config features. Much better approach than what I was trying to do.

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020