Feb-08-2017, 08:04 AM
Hi,
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 "destination.py" 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 destination.py (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 "destination.py' 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.)
destination.py code:
main.py code:
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 "destination.py" 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 destination.py (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 "destination.py' 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.)
destination.py code:
1 |
userPath: '' |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
from kivy.config import Config Config. set ( 'graphics' , 'width' , '700' ) Config. set ( 'graphics' , 'height' , '700' ) Config. set ( 'graphics' , 'fullscreen' , 'fake' ) from kivy.app 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 kivy.properties 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 kivy.properties 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. Builder.load_string( """ #:import C kivy.utils.get_color_from_hex #:import WipeTransition kivy.uix.screenmanager.WipeTransition <DropFile>: <RunButton>: #color: .5,.6,.2,.7 <FileScreen>: MainPageGrid: padding:1 spacing: 1 MainPageButton: #background_color: C('#bfbf40') text: "Back to Menu" on_release: app.root.current = 'main' MainPageButton: background_color: C('#bfbf40') text: "Change Folder" on_release: root.set_folder() MainPageButton: background_color: C('#bfbf40') text: "Show Current Folder" on_release: root.popup_current() BoxLayout: orientation: "vertical" FileChooserListView: 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() TextInput: size_hint: 1, .1 text: root.new_path <MainPageGrid@GridLayout>: cols: 2 spacing: 10 row_default_height: (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 <MainPageButton@Button>: background_normal: 'button_normal.png' background_down: 'button_down.png' background_color: C('#95A5A6') font_size: 24 #halign: 'center' markup: True <Button>: background_normal: 'button_normal.png' background_down: 'button_down.png' background_color: C('#95A5A6') font_size: 24 markup: True halign: 'center' <PopupCurrent>: size_hint: .8, .4 title: "Current Folder" BoxLayout: orientation: 'vertical' ScrollView: Label: 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 Button: 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 ): reload (destination) PopupCurrent(). open () def show_selection( self ): sel = self .ids[ "filechooser" ] try : self .new_path = sel.selection[ 0 ] print self .new_path return self .new_path except : pass def is_dir( self , directory, filename): return isdir(join(directory, filename)) def set_folder( self ): directory = self .new_path f = open ( 'destination.py' , 'w' ) p = "userPath='" + directory + "'" f.write(p) f.close() print p reload (destination) class PopupCurrent(Popup): reload (destination) msg = StringProperty() msg = destination.userPath class TestApp(App): def build( self ): # Create the screen manager self .drops = [] sm = ScreenManager() sm.add_widget(FileScreen(name = 'folder' )) Window.bind(on_dropfile = self .handledrops) return sm def handledrops( self , * args): for i in self .drops: i( * args) if __name__ = = '__main__' : TestApp().run() |