如何更改Kivy App的配置文件默认目录?
How to change a Kivy App's configuration file default directory?
我正在尝试更改保存我的 Kivy 应用程序 '%(appname)s.ini'
文件的目录。
我可以使用 get_application_config()
获取当前目录,但即使我更改 defaultpath
属性也没有任何反应。我也尝试设置 'KIVY_HOME'
环境变量,但我一定是做错了什么。
代码
#These are the three methods I tried
#Method 1 change KIVY_HOME environment at beginning before importing App class
import kivy
import os, plyer
con_change = plyer.storagepath.get_documents_dir()
os.environ['KIVY_HOME'] = con_change + '/'
#Method 2 change KIVY_HOME environment within App class
import kivy
import os, plyer
from kivy.app import App
from kivy.properties import ConfigParserProperty
class LoginApp(App):
title = 'Login'
us_er = ConfigParserProperty('',
'mylog', 'uname', 'app')
p_wd = ConfigParserProperty('',
'mylog', 'pwd', 'app')
con_change = plyer.storagepath.get_documents_dir()
def build(self):
self.config.export KIVY_HOME = self.con_change + '/%(appname)s.ini' #gives invalid syntax
up = self.config.items('mylog')
return TheLogger()
def build_config(self, config):
config.setdefaults('mylog', {
'uname': '',
'pwd': ''})
#Method 3 try to set defaultpath with get_application_path function
import kivy
import os, plyer
from kivy.app import App
class LoginApp(App):
title = 'Login'
us_er = ConfigParserProperty('',
'mylog', 'uname', 'app')
p_wd = ConfigParserProperty('',
'mylog', 'pwd', 'app')
con_change = plyer.storagepath.get_documents_dir()
def build(self):
self.get_application_config(defaultpath= self.con_change + '/%(appname)s.ini')
up = self.config.items('mylog')
return TheLogger()
def build_config(self, config):
config.setdefaults('mylog', {
'uname': '',
'pwd': ''})
我希望能够在 'KIVY_HOME'
目录树之外设置 '%(appname)s.ini'
文件的目录?
根据 documentation,如果您在应用程序启动时读取配置文件,则相同的配置文件将用于 Config.write()
方法。所以,如果(在 Python 文件的顶部)你这样做:
from kivy.config import Config
import os.path
def get_config_file_name():
# return any file name here
return str(os.path.join(os.path.expanduser('~'), 'myApp.ini'))
Config.read(get_config_file_name())
然后执行任何 Config.set
(或其他任何操作),然后只需执行 Config.write()
即可将更改保存到配置文件。
我正在尝试更改保存我的 Kivy 应用程序 '%(appname)s.ini'
文件的目录。
我可以使用 get_application_config()
获取当前目录,但即使我更改 defaultpath
属性也没有任何反应。我也尝试设置 'KIVY_HOME'
环境变量,但我一定是做错了什么。
代码
#These are the three methods I tried
#Method 1 change KIVY_HOME environment at beginning before importing App class
import kivy
import os, plyer
con_change = plyer.storagepath.get_documents_dir()
os.environ['KIVY_HOME'] = con_change + '/'
#Method 2 change KIVY_HOME environment within App class
import kivy
import os, plyer
from kivy.app import App
from kivy.properties import ConfigParserProperty
class LoginApp(App):
title = 'Login'
us_er = ConfigParserProperty('',
'mylog', 'uname', 'app')
p_wd = ConfigParserProperty('',
'mylog', 'pwd', 'app')
con_change = plyer.storagepath.get_documents_dir()
def build(self):
self.config.export KIVY_HOME = self.con_change + '/%(appname)s.ini' #gives invalid syntax
up = self.config.items('mylog')
return TheLogger()
def build_config(self, config):
config.setdefaults('mylog', {
'uname': '',
'pwd': ''})
#Method 3 try to set defaultpath with get_application_path function
import kivy
import os, plyer
from kivy.app import App
class LoginApp(App):
title = 'Login'
us_er = ConfigParserProperty('',
'mylog', 'uname', 'app')
p_wd = ConfigParserProperty('',
'mylog', 'pwd', 'app')
con_change = plyer.storagepath.get_documents_dir()
def build(self):
self.get_application_config(defaultpath= self.con_change + '/%(appname)s.ini')
up = self.config.items('mylog')
return TheLogger()
def build_config(self, config):
config.setdefaults('mylog', {
'uname': '',
'pwd': ''})
我希望能够在 'KIVY_HOME'
目录树之外设置 '%(appname)s.ini'
文件的目录?
根据 documentation,如果您在应用程序启动时读取配置文件,则相同的配置文件将用于 Config.write()
方法。所以,如果(在 Python 文件的顶部)你这样做:
from kivy.config import Config
import os.path
def get_config_file_name():
# return any file name here
return str(os.path.join(os.path.expanduser('~'), 'myApp.ini'))
Config.read(get_config_file_name())
然后执行任何 Config.set
(或其他任何操作),然后只需执行 Config.write()
即可将更改保存到配置文件。