有可能有几种方法来做到这一点。这里是其中的一个:
创建一个新的类型设置的,它接受一个函数作为字符串,将包含你希望每个用户想要看列表时,调用该功能的完整路径:
class SettingDynamicOptions(SettingOptions):
'''Implementation of an option list that creates the items in the possible
options list by calling an external method, that should be defined in
the settings class.
'''
function_string = StringProperty()
'''The function's name to call each time the list should be updated.
It should return a list of strings, to be used for the options.
'''
def _create_popup(self, instance):
# Update the options
mod_name, func_name = self.function_string.rsplit('.',1)
mod = importlib.import_module(mod_name)
func = getattr(mod, func_name)
self.options = func()
# Call the parent __init__
super(SettingDynamicOptions, self)._create_popup(instance)
它是从SettingOptions中的子类,它允许用户从下拉列表中进行选择。每次用户按下设置以查看可能的选项时,都会调用_create_popup
方法。新的overriden方法动态导入该函数并调用它来更新类的选项属性(这反映在下拉列表中)。
现在,可以在JSON来创建一个这样的设置项:
{
"type": "dynamic_options",
"title": "options that are always up to date",
"desc": "some desc.",
"section": "comm",
"key": "my_dynamic_options",
"function_string": "my_module.my_sub_module.my_function"
},
也是必须通过继承Kivy的设置类注册新的设置类型:
class MySettings(SettingsWithSidebar):
'''Customized settings panel.
'''
def __init__(self, *args, **kargs):
super(MySettings, self).__init__(*args, **kargs)
self.register_type('dynamic_options', SettingDynamicOptions)
,并用它来您的应用:
def build(self):
'''Build the screen.
'''
self.settings_cls = MySettings