如何从 BoxLayout 重新定位小部件
How to reposition Widgets from BoxLayout
我正在使用最新版本的 kivy: v1.10.1.dev0, git-Unknown, 20171208
, python v3.6.2
, mac os.
我想实现以下目标:当我使用更改后的设置更新设置 reposition/redraw BoxLayout
中的值时,在我的例子中,我正在更新 size_hint
值。这是我目前的工作:
my.py
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"
}
]
那么,当我 close 设置 window 时,如何看到新布局更新为 size_hint
的新值?
谢谢!
好的,在 IRC #Kivy 的帮助下,解决方案是不在 .kv
文件中直接使用 app.config.get('layout', 'size_hint')
,而是使用 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'
my.py
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()
我正在使用最新版本的 kivy: v1.10.1.dev0, git-Unknown, 20171208
, python v3.6.2
, mac os.
我想实现以下目标:当我使用更改后的设置更新设置 reposition/redraw BoxLayout
中的值时,在我的例子中,我正在更新 size_hint
值。这是我目前的工作:
my.py
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"
}
]
那么,当我 close 设置 window 时,如何看到新布局更新为 size_hint
的新值?
谢谢!
好的,在 IRC #Kivy 的帮助下,解决方案是不在 .kv
文件中直接使用 app.config.get('layout', 'size_hint')
,而是使用 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'
my.py
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()