python - 使用更改的 Config 更新 Kivy 设置面板值
问题描述
问题:
我正在构建一个应用程序并使用 kivy 设置面板来更改设置。一些设置存储在 ConfigParserProperties 中,也可以在设置面板之外进行更改。当我重新打开设置面板时,设置面板中的值仍然是旧值。
如何使用最新值(在 ConfigParserProperties 中)更新设置面板?
我试过的:
我发现了这个动态更新的 Kivy 设置条目但是这里只创建了 1 个更新的小部件(列表弹出窗口)。我正在寻找一种方法来更新不同类型的多个设置项。
我让设置面板更新的唯一方法是销毁它并在每次打开它时重新创建它(代码见下文)。如果面板包含多个页面(为了简单起见,此示例中并非如此),这是一个(明显)缓慢的过程。
例子:
此示例为变量 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 中的更改更改/更新特定面板项目的方法,非常感谢。
解决方案
推荐阅读
- javascript - 响应 cookie 未在 Fetch 请求中发送
- python - 安装完成之前tenserflow相当
- python - 打印矩阵的元素,每行换行,每行的元素用空格分隔
- google-cloud-platform - 从另一个项目访问 Cloud Run 服务
- python - tensorflow,训练后拆分自动编码器
- google-sheets - 找不到计算具有多个条件(OR 和 AND)的列的公式
- c# - Google Cloud Storage api (c#) - 缓存标头元数据
- python - 使用 healpy 和 projplot 进行 Aitoff 投影
- c - 堆栈粉碎检测到 STM32f7
- java - 使用 javac 编译 Java 文件集时缺少类文件