【问题标题】:How to reposition Widgets from BoxLayout如何从 BoxLayout 重新定位小部件
【发布时间】:2017-12-17 16:37:58
【问题描述】:

我正在使用最新版本的 kivy:v1.10.1.dev0, git-Unknown, 20171208,python v3.6.2,mac os。

我想要实现以下目标:当我使用更改后的设置更新设置重新定位/重绘BoxLayout 中的值时,在我的情况下,我正在更新size_hint 值。这是我到目前为止的工作:

我的.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()

我的.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'

我的.ini

[layout]
size_hint = 0.6

我的.json

[
  {
        "type": "string",
        "title": "Size Hint",
        "desc": "Size Hint",
        "section": "layout",
        "key": "size_hint"
    }
]

那么,当我关闭设置窗口时,如何看到新布局更新为 size_hint 的新值?

谢谢!

【问题讨论】:

    标签: kivy kivy-language


    【解决方案1】:

    好的,在 IRC #Kivy 的帮助下,解决方案是不在.kv 文件中直接使用app.config.get('layout', 'size_hint'),而是使用Properties

    我的.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'
    

    我的.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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      相关资源
      最近更新 更多