首页 > 解决方案 > 使用更改的 Config 更新 Kivy 设置面板值

问题描述

问题:

我正在构建一个应用程序并使用 kivy 设置面板来更改设置。一些设置存储在 ConfigParserProperties 中,也可以在设置面板之外进行更改。当我重新打开设置面板时,设置面板中的值仍然是旧值。
如何使用最新值(在 ConfigParserProperties 中)更新设置面板?

我试过的:

例子:

此示例为变量 test 生成一个随机值,这是设置面板中的唯一项目(通过单击按钮或按 f1 打开它)。如您所见,该值不会从起始值更改。

from kivy.app import App
from kivy.properties import ConfigParserProperty
from random import randint
from kivy.clock import Clock
from kivy.uix.button import Button
import json


class MyApp(App):
    test = ConfigParserProperty(0, 'testing', 'test_par', 'app_config', val_type=int)
    use_kivy_settings = False   # disable kivy settings in options

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        Clock.schedule_interval(self.new_number, 1)

    def build(self):        
        self.config.read("test.ini")
        self.config.name = "app_config"
        _app = super().build()
        _app.add_widget(Button(text='open settings', 
                               size=_app.size, 
                               pos=_app.pos, 
                               on_release=self.open_settings))
        return _app

    def build_settings(self, settings):
        _setts = json.dumps([
        {
            "type": "title",
            "title": "Test Settings"
        },
        {
            "type": "numeric",
            "title": "Test Var",
            "desc": "Randomly changing test var, wish it would change here too",
            "section": "testing",
            "key": "test_par"
        }])
        settings.add_json_panel('Test Panel', self.config, data=_setts)
        self.settings = settings

    def new_number(self, *args, **kwargs):
        # generate random new values for test
        self.test = randint(0, 0xff)
        print(self.test)

if __name__ == "__main__":
    app = MyApp()
    app.run()

我尝试了什么:
添加此代码将在每次打开面板时更改值。但每次打开面板时都会破坏并重新创建面板。多个面板项目的速度很慢。

    def open_settings(self, *args):
        self.destroy_settings()  # clear settings
        return super().open_settings(*args)

如果有人知道通过 ConfigParserProperties 中的更改更改/更新特定面板项目的方法,非常感谢。

标签: pythonkivy

解决方案


推荐阅读