我正在使用最新版本的kivy:v1.10.1.dev0, git-Unknown, 20171208
,python v3.6.2
,mac os。
我想要实现以下内容:当我使用更改的设置更新设置重新定位/重绘BoxLayout
中的值时,在我的情况下,我正在更新size_hint
值。这是我到目前为止的工作:
没有.朋友
from kivy.app import App
class MyApp(App):
def build_config(self, config):
config.read('./my.ini')
def build_settings(self, settings):
settings.add_json_panel('Test', self.config, filename='my.json')
def on_config_change(self, config, section, key, value):
if section in ('layout',) and key in ('size_hint',):
# NOT WORKING!!
# print(self.root.canvas.clear())
# self.root.canvas.ask_update()
# self.root.canvas.draw()
# NOT WORKING!!
print(self.root)
print(self.root.do_layout())
if __name__ == "__main__":
MyApp().run()
my.kv
#:import SettingsWithSidebar kivy.uix.settings.SettingsWithSidebar
BoxLayout:
RecycleView:
size_hint: float(app.config.get('layout', 'size_hint')), 1
Button:
text: 'Open Settings'
on_touch_down: app.settings_cls = SettingsWithSidebar; app.open_settings()
RecycleView:
size_hint: 1-float(app.config.get('layout', 'size_hint')), 1
Button:
text: 'Just a test'
的my.ini
[layout]
size_hint = 0.6
my.json
[
{
"type": "string",
"title": "Size Hint",
"desc": "Size Hint",
"section": "layout",
"key": "size_hint"
}
]
那么,当我关闭设置窗口时,如何看到使用size_hint
的新值更新的新布局?
谢谢!
好的,在IRC #Kivy的一些帮助之后,解决方法是不在app.config.get('layout', 'size_hint')
文件中直接使用.kv
,而是使用Properties
代替。
my.kv
BoxLayout:
RecycleView:
size_hint: app.size_hint, 1
Button:
text: 'Open Settings'
on_touch_down: app.open_settings()
RecycleView:
size_hint: 1-app.size_hint, 1
Button:
text: 'Just a test'
没有.朋友
from kivy.app import App
from kivy.properties import NumericProperty
from kivy.uix.settings import SettingsWithSidebar
class MyApp(App):
settings_cls = SettingsWithSidebar
size_hint = NumericProperty()
def build_config(self, config):
config.read('./my.ini')
def build_settings(self, settings):
settings.add_json_panel('Test', self.config, filename='my.json')
def load_config(self):
config = super(MyApp, self).load_config()
self.size_hint = config.getfloat('layout', 'size_hint')
return config
def on_config_change(self, config, section, key, value):
if section in ('layout',) and key in ('size_hint',):
self.size_hint = value
if __name__ == '__main__':
MyApp().run()