Python 动态更新的Kivy设置条目

Python 动态更新的Kivy设置条目,python,kivy,Python,Kivy,Kivy有这个很棒的内置功能,可以为你的应用程序创建一个设置面板。 它提供了一组可以使用的条目类型,如字符串、bool、选项等。 但是所有这些选项都是在json文件中硬编码的,如果有动态的事情发生,你会怎么做 如何在Kivy中动态更改设置菜单 具体来说,我需要有一个串行连接设置面板。我的应用程序的用户需要选择要连接的现有串行端口。此列表可以在python中获得,但它可以随时更改,因此如何使“设置”菜单与当前的com端口可用性保持同步?可能有几种方法。以下是其中之一: 创建一种新类型的设置,将函数

Kivy有这个很棒的内置功能,可以为你的应用程序创建一个设置面板。 它提供了一组可以使用的条目类型,如字符串、bool、选项等。 但是所有这些选项都是在json文件中硬编码的,如果有动态的事情发生,你会怎么做

如何在Kivy中动态更改设置菜单


具体来说,我需要有一个串行连接设置面板。我的应用程序的用户需要选择要连接的现有串行端口。此列表可以在python中获得,但它可以随时更改,因此如何使“设置”菜单与当前的com端口可用性保持同步?

可能有几种方法。以下是其中之一:

创建一种新类型的设置,将函数作为字符串接受,该设置将包含每次用户希望查看列表时要调用的函数的完整路径:

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的子类,用户可以从下拉列表中进行选择。每次用户按下设置以查看可能的选项时,都会调用
\u create\u popup
方法。新的重写方法动态导入函数并调用它来更新类的options属性(反映在下拉列表中)

现在可以在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

可能有几种方法可以做到这一点。以下是其中之一:

创建一种新类型的设置,将函数作为字符串接受,该设置将包含每次用户希望查看列表时要调用的函数的完整路径:

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的子类,用户可以从下拉列表中进行选择。每次用户按下设置以查看可能的选项时,都会调用
\u create\u popup
方法。新的重写方法动态导入函数并调用它来更新类的options属性(反映在下拉列表中)

现在可以在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