Python kivy(kivymd)如何从MD存储和加载数据

Posted

技术标签:

【中文标题】Python kivy(kivymd)如何从MD存储和加载数据【英文标题】:Python kivy (kivymd) how to store and load data from MD 【发布时间】:2019-01-14 20:53:55 【问题描述】:

我想存储数据并从 TextField 加载数据(这里是来自 kivymd 的 MDTextField)。我添加了 on_application_close: ... 和 on_application_open: ... 作为原型,因为我不知道如何做以及如何将它连接到包含函数的 main.py 文件。所以我希望用户能够在 TextField 中输入一些值,如果用户关闭并重新打开应用程序,这些值应该仍然存在。我在文档中查找了存储功能,但我不清楚如何正确使用它。

MDTextField:
    id: first_text_field
    hint_text: "Helper text on focus"
    helper_text: "This will disappear when you click off"
    helper_text_mode: "on_focus"
    input_filter: "int"
    on_application_close: store_content(first_text_field, first_text_field.text)
    on_application_open: set_text(load_content(first_text_field))

编辑:这是更新后的代码(main.py、main.kv 和 labels.py)。所有文件都在项目文件夹的文件夹应用程序中。我还在同一个文件夹中添加了一个空的 myBackup.json。

这是labels.py文件

# labels.py
APPLICATION_NAME = "Application"
CLOSE_APPLICATION = "Close Application"
NAVIGATION = "Navigation"
SYSTEM_PARAMETERS = "System Parameters"
TEXT_ON_CLOSE_APPLICATION = "Are your sure?"
TEXT_ON_CLOSE_APPLICATION_BUTTON_CLOSE = "Close Application"

CONTROLLER_PARAMETER_1_HINT_TEXT = "Controller Parameter 1"
CONTROLLER_PARAMETER_1_HELPER_TEXT = "This parameter controls damping ratio."
CONTROLLER_PARAMETER_2_HINT_TEXT = "Controller Parameter 2"
CONTROLLER_PARAMETER_2_HELPER_TEXT = "This parameter controls P component of the PID controller."

更新代码:我没有收到错误,但是通过窗口的关闭按钮(X 按钮)退出应用程序后 myBackup.json 文件保持为空。

# main.py
# -*- coding: utf-8 -*-

import os

# Set virtual keyboard
from kivy.app import App
from kivy.config import Config
from kivy.storage.jsonstore import JsonStore
from kivy.uix.boxlayout import BoxLayout

Config.set('kivy', 'keyboard_mode', 'systemandmulti')
from kivy.uix.popup import Popup
from kivy.uix.button import Button


from kivy.core.window import Window
# Window.fullscreen = "auto"
from kivy.lang import Builder
from functools import partial


from kivymd.list import BaseListItem
from kivymd.material_resources import DEVICE_TYPE
from kivymd.navigationdrawer import MDNavigationDrawer, NavigationDrawerHeaderBase
from kivymd.theming import ThemeManager

from application.labels import *


class HackedDemoNavDrawer(MDNavigationDrawer):
    # DO NOT USE
    def add_widget(self, widget, index=0):
        if issubclass(widget.__class__, BaseListItem):
            self._list.add_widget(widget, index)
            if len(self._list.children) == 1:
                widget._active = True
                self.active_item = widget
            # widget.bind(on_release=lambda x: self.panel.toggle_state())
            widget.bind(on_release=lambda x: x._set_active(True, list=self))
        elif issubclass(widget.__class__, NavigationDrawerHeaderBase):
            self._header_container.add_widget(widget)
        else:
            super(MDNavigationDrawer, self).add_widget(widget, index)


class MainApp(App):

    theme_cls = ThemeManager()
    title = APPLICATION_NAME

    def build(self):
        main_widget = Builder.load_file(
            os.path.join(os.path.dirname(__file__), "./main.kv")
        )
        self.theme_cls.theme_style = 'Dark'

        main_widget.ids.text_field_error.bind(
            on_text_validate=self.set_error_message,
            on_focus=self.set_error_message)
        self.bottom_navigation_remove_mobile(main_widget)
        return main_widget

    def stop(self, *largs):
        # Open the popup you want to open and declare callback if user pressed `Yes`
        popup = ExitPopup(title=TEXT_ON_CLOSE_APPLICATION,
                          content=Button(text=TEXT_ON_CLOSE_APPLICATION_BUTTON_CLOSE),
                          size=(400, 400), size_hint=(None, None)
                          )
        popup.bind(on_confirm=partial(self.close_app, *largs))
        popup.open()

    def close_app(self, *largs):
        super(MainApp, self).stop(*largs)

    def bottom_navigation_remove_mobile(self, widget):
        # Removes some items from bottom-navigation demo when on mobile
        if DEVICE_TYPE == 'mobile':
            widget.ids.bottom_navigation_demo.remove_widget(widget.ids.bottom_navigation_desktop_2)
        if DEVICE_TYPE == 'mobile' or DEVICE_TYPE == 'tablet':
            widget.ids.bottom_navigation_demo.remove_widget(widget.ids.bottom_navigation_desktop_1)

    def set_error_message(self, *args):
        if len(self.root.ids.text_field_error.text) == 2:
            self.root.ids.text_field_error.error = True
        else:
            self.root.ids.text_field_error.error = False

    def on_pause(self):
        return True

    def on_text_validate_callback(self, instance):
        print(instance.text)

    def on_start(self):
        print("\non_start:")
        store = JsonStore('myBackup.json')
        if store.count() > 0:
            for key in store:
                print("\tid=0, obj=1".format(key, self.root.ids[key]))
                if isinstance(self.root.ids[key], MDTextField):
                    self.root.ids[key].text = store.get(key)['text']
                    print("\t\ttext=", self.root.ids[key].text)

    def on_stop(self):
        print("\non_stop:")
        store = JsonStore('myBackup.json')
        for key in self.root.ids:
            if isinstance(self.root.ids[key], MDTextField):
                print("\tid=0, text=1".format(key, self.root.ids[key].text))
                store.put(key, text=self.root.ids[key].text)


class ExitPopup(Popup):

    def __init__(self, **kwargs):
        super(ExitPopup, self).__init__(**kwargs)
        self.register_event_type('on_confirm')

    def on_confirm(self):
        pass

    def on_button_yes(self):
        self.dispatch('on_confirm')


if __name__ == '__main__':
    MainApp().run()

这是 main.kv 文件。 labels.py 文件不变

# main.kv
#:kivy 1.10.1
#:import Toolbar kivymd.toolbar.Toolbar
#:import MDNavigationDrawer application.kivymd.navigationdrawer.MDNavigationDrawer
#:import NavigationLayout application.kivymd.navigationdrawer.NavigationLayout
#:import NavigationDrawerDivider application.kivymd.navigationdrawer.NavigationDrawerDivider
#:import NavigationDrawerToolbar application.kivymd.navigationdrawer.NavigationDrawerToolbar
#:import NavigationDrawerSubheader application.kivymd.navigationdrawer.NavigationDrawerSubheader
#:import MDTextField application.kivymd.textfields.MDTextField
#:import labels application.labels

NavigationLayout:
    id: nav_layout
    MDNavigationDrawer:
        id: nav_drawer
        NavigationDrawerToolbar:
            title: labels.NAVIGATION
        NavigationDrawerIconButton:
            icon: 'checkbox-blank-circle'
            text: labels.SYSTEM_PARAMETERS
            on_release: app.root.ids.scr_mngr.current = 'system_parameters'
        NavigationDrawerIconButton:
            icon: "checkbox-blank-circle"
            text: labels.CLOSE_APPLICATION
            on_release: app.stop()
    BoxLayout:
        orientation: 'vertical'
        halign: "center"
        Toolbar:
            id: toolbar
            title: labels.APPLICATION_NAME
            md_bg_color: app.theme_cls.primary_color
            background_palette: 'Primary'
            background_hue: '500'
            left_action_items: [['menu', lambda x: app.root.toggle_nav_drawer()]]
            #right_action_items: [['dots-vertical', lambda x: app.root.toggle_nav_drawer()]]
        ScreenManager:
            id: scr_mngr
            Screen:
                name: 'system_parameters'
                BoxLayout:
                    orientation: "horizontal"
                    BoxLayout:
                        orientation: 'vertical'
                        size_hint_y: None
                        height: self.minimum_height
                        padding: dp(48)
                        spacing: 10
                        MDTextField:
                            id: controller_parameter_1
                            hint_text: labels.CONTROLLER_PARAMETER_1_HINT_TEXT
                            helper_text: labels.CONTROLLER_PARAMETER_1_HELPER_TEXT
                            helper_text_mode: "on_focus"
                            input_filter: "int"
                            on_text_validate: app.on_text_validate_callback(self)
                    BoxLayout:
                        orientation: 'vertical'
                        size_hint_y: None
                        height: self.minimum_height
                        padding: dp(48)
                        spacing: 10
                        MDTextField:
                            id: controller_parameter_2
                            hint_text: labels.CONTROLLER_PARAMETER_2_HINT_TEXT
                            helper_text: labels.CONTROLLER_PARAMETER_2_HELPER_TEXT
                            helper_text_mode: "on_focus"
                            input_filter: "float"
                            on_text_validate: app.on_text_validate_callback(self)

            Screen:
                name: 'textfields'
                ScrollView:
                    BoxLayout:
                        orientation: 'vertical'
                        MDTextField:
                            id: text_field_error
                            hint_text: "Helper text on error (Hit Enter with two characters here)"
                            helper_text: "Two is my least favorite number"
                            helper_text_mode: "on_error"

            Screen:
                name: 'nav_drawer'
                HackedDemoNavDrawer:
                    # NavigationDrawerToolbar:
                    #     title: "Navigation Drawer Widgets"
                    NavigationDrawerIconButton:
                        icon: 'checkbox-blank-circle'
                        text: "Badge text ---->"
                        badge_text: "99+"
                    NavigationDrawerIconButton:
                        active_color_type: 'accent'
                        text: "Accent active color"
                    NavigationDrawerIconButton:
                        active_color_type: 'custom'
                        text: "Custom active color"
                        active_color: [1, 0, 1, 1]
                    NavigationDrawerIconButton:
                        use_active: False
                        text: "Use active = False"
                    NavigationDrawerIconButton:
                        text: "Different icon"
                        icon: 'alarm'
                    NavigationDrawerDivider:
                    NavigationDrawerSubheader:
                        text: "NavigationDrawerSubheader"
                    NavigationDrawerIconButton:
                        text: "NavigationDrawerDivider \/"
                    NavigationDrawerDivider:

【问题讨论】:

【参考方案1】:

问题 2

我没有收到错误,但 myBackup.json 文件在之后保持为空 通过窗口的关闭按钮(X 按钮)退出应用程序。

解决方案 2

使用Window.bind(on_close=self.on_stop)

示例 2

app.py

​​>
# app.py
# -*- coding: utf-8 -*-

import os
from functools import partial

from kivy.app import App
from kivy.storage.jsonstore import JsonStore
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.lang import Builder

from kivy.config import Config

# Set virtual keyboard
Config.set('kivy', 'keyboard_mode', 'systemandmulti')

from kivy.core.window import Window
# Window.fullscreen = "auto"

from kivymd.list import BaseListItem
from kivymd.material_resources import DEVICE_TYPE
from kivymd.navigationdrawer import MDNavigationDrawer, NavigationDrawerHeaderBase
from kivymd.textfields import MDTextField
from kivymd.theming import ThemeManager

from labels import *


class HackedDemoNavDrawer(MDNavigationDrawer):
    # DO NOT USE
    def add_widget(self, widget, index=0):
        if issubclass(widget.__class__, BaseListItem):
            self._list.add_widget(widget, index)
            if len(self._list.children) == 1:
                widget._active = True
                self.active_item = widget
            # widget.bind(on_release=lambda x: self.panel.toggle_state())
            widget.bind(on_release=lambda x: x._set_active(True, list=self))
        elif issubclass(widget.__class__, NavigationDrawerHeaderBase):
            self._header_container.add_widget(widget)
        else:
            super(MDNavigationDrawer, self).add_widget(widget, index)


class MainApp(App):

    theme_cls = ThemeManager()
    title = APPLICATION_NAME

    def __init__(self, **kwargs):
        super(MainApp, self).__init__(**kwargs)
        Window.bind(on_close=self.on_stop)

    def build(self):
        main_widget = Builder.load_file(
            os.path.join(os.path.dirname(__file__), "./main.kv")
        )
        self.theme_cls.theme_style = 'Dark'

        main_widget.ids.text_field_error.bind(
            on_text_validate=self.set_error_message,
            on_focus=self.set_error_message)
        self.bottom_navigation_remove_mobile(main_widget)
        return main_widget

    def stop(self, *largs):
        # Open the popup you want to open and declare callback if user pressed `Yes`
        popup = ExitPopup(title=TEXT_ON_CLOSE_APPLICATION,
                          content=Button(text=TEXT_ON_CLOSE_APPLICATION_BUTTON_CLOSE),
                          size=(400, 400), size_hint=(None, None)
                          )
        popup.bind(on_confirm=partial(self.close_app, *largs))
        popup.open()

    def close_app(self, *largs):
        super(MainApp, self).stop(*largs)

    def bottom_navigation_remove_mobile(self, widget):
        # Removes some items from bottom-navigation demo when on mobile
        if DEVICE_TYPE == 'mobile':
            widget.ids.bottom_navigation_demo.remove_widget(widget.ids.bottom_navigation_desktop_2)
        if DEVICE_TYPE == 'mobile' or DEVICE_TYPE == 'tablet':
            widget.ids.bottom_navigation_demo.remove_widget(widget.ids.bottom_navigation_desktop_1)

    def set_error_message(self, *args):
        if len(self.root.ids.text_field_error.text) == 2:
            self.root.ids.text_field_error.error = True
        else:
            self.root.ids.text_field_error.error = False

    def on_pause(self):
        return True

    def on_text_validate_callback(self, instance):
        print(instance.text)

    def on_start(self):
        print("\non_start:")
        store = JsonStore('myBackup.json')
        if store.count() > 0:
            for key in store:
                print("\tid=0, obj=1".format(key, self.root.ids[key]))
                if isinstance(self.root.ids[key], MDTextField):
                    self.root.ids[key].text = store.get(key)['text']
                    print("\t\ttext=", self.root.ids[key].text)

    def on_stop(self, *args):
        print("\non_stop:")
        store = JsonStore('myBackup.json')
        for key in self.root.ids:
            if isinstance(self.root.ids[key], MDTextField):
                print("\tid=0, text=1".format(key, self.root.ids[key].text))
                store.put(key, text=self.root.ids[key].text)


class ExitPopup(Popup):

    def __init__(self, **kwargs):
        super(ExitPopup, self).__init__(**kwargs)
        self.register_event_type('on_confirm')

    def on_confirm(self):
        pass

    def on_button_yes(self):
        self.dispatch('on_confirm')


if __name__ == '__main__':
    MainApp().run()

main.kv

#:kivy 1.11.0
#:import Toolbar kivymd.toolbar.Toolbar
#:import MDNavigationDrawer kivymd.navigationdrawer.MDNavigationDrawer
#:import NavigationLayout kivymd.navigationdrawer.NavigationLayout
#:import NavigationDrawerDivider kivymd.navigationdrawer.NavigationDrawerDivider
#:import NavigationDrawerToolbar kivymd.navigationdrawer.NavigationDrawerToolbar
#:import NavigationDrawerSubheader kivymd.navigationdrawer.NavigationDrawerSubheader
#:import MDTextField kivymd.textfields.MDTextField
#:import labels labels

NavigationLayout:
    id: nav_layout
    MDNavigationDrawer:
        id: nav_drawer
        NavigationDrawerToolbar:
            title: labels.NAVIGATION
        NavigationDrawerIconButton:
            icon: 'checkbox-blank-circle'
            text: labels.SYSTEM_PARAMETERS
            on_release: app.root.ids.scr_mngr.current = 'system_parameters'
        NavigationDrawerIconButton:
            icon: "checkbox-blank-circle"
            text: labels.CLOSE_APPLICATION
            on_release: app.stop()
    BoxLayout:
        orientation: 'vertical'
        halign: "center"
        Toolbar:
            id: toolbar
            title: labels.APPLICATION_NAME
            md_bg_color: app.theme_cls.primary_color
            background_palette: 'Primary'
            background_hue: '500'
            left_action_items: [['menu', lambda x: app.root.toggle_nav_drawer()]]
            #right_action_items: [['dots-vertical', lambda x: app.root.toggle_nav_drawer()]]
        ScreenManager:
            id: scr_mngr
            Screen:
                name: 'system_parameters'
                BoxLayout:
                    orientation: "horizontal"
                    BoxLayout:
                        orientation: 'vertical'
                        size_hint_y: None
                        height: self.minimum_height
                        padding: dp(48)
                        spacing: 10
                        MDTextField:
                            id: controller_parameter_1
                            hint_text: labels.CONTROLLER_PARAMETER_1_HINT_TEXT
                            helper_text: labels.CONTROLLER_PARAMETER_1_HELPER_TEXT
                            helper_text_mode: "on_focus"
                            input_filter: "int"
                            on_text_validate: app.on_text_validate_callback(self)      # Fix 1
                    BoxLayout:
                        orientation: 'vertical'
                        size_hint_y: None
                        height: self.minimum_height
                        padding: dp(48)
                        spacing: 10
                        MDTextField:
                            id: controller_parameter_2
                            hint_text: labels.CONTROLLER_PARAMETER_2_HINT_TEXT
                            helper_text: labels.CONTROLLER_PARAMETER_2_HELPER_TEXT
                            helper_text_mode: "on_focus"
                            input_filter: "float"
                            on_text_validate: app.on_text_validate_callback(self)      # Fix 2

            Screen:
                name: 'textfields'
                ScrollView:
                    BoxLayout:
                        orientation: 'vertical'
                        MDTextField:
                            id: text_field_error
                            hint_text: "Helper text on error (Hit Enter with two characters here)"
                            helper_text: "Two is my least favorite number"
                            helper_text_mode: "on_error"

            Screen:
                name: 'nav_drawer'
                HackedDemoNavDrawer:
                    # NavigationDrawerToolbar:
                    #     title: "Navigation Drawer Widgets"
                    NavigationDrawerIconButton:
                        icon: 'checkbox-blank-circle'
                        text: "Badge text ---->"
                        badge_text: "99+"
                    NavigationDrawerIconButton:
                        active_color_type: 'accent'
                        text: "Accent active color"
                    NavigationDrawerIconButton:
                        active_color_type: 'custom'
                        text: "Custom active color"
                        active_color: [1, 0, 1, 1]
                    NavigationDrawerIconButton:
                        use_active: False
                        text: "Use active = False"
                    NavigationDrawerIconButton:
                        text: "Different icon"
                        icon: 'alarm'
                    NavigationDrawerDivider:
                    NavigationDrawerSubheader:
                        text: "NavigationDrawerSubheader"
                    NavigationDrawerIconButton:
                        text: "NavigationDrawerDivider \/"
                    NavigationDrawerDivider:

输出


详情请参考示例。

    使用Kivy Storage 使用应用程序事件,on_starton_stop。 添加导入语句,from kivymd.textfields import MDTextField

App events

on_start:

在应用程序启动时触发(在 runTouchApp() 之前) 打电话。

on_stop:

应用程序停止时触发。

示例

main.py

​​>
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivymd.theming import ThemeManager
from kivymd.textfields import MDTextField
from kivy.storage.jsonstore import JsonStore


class RootWidget(BoxLayout):
     def on_text_validate_callback(self, instance):
         print(instance.text)


class TestApp(App):
    theme_cls = ThemeManager()

    def build(self):
        return RootWidget()

    def on_start(self):
        print("\non_start:")
        store = JsonStore('myBackup.json')
        if store.count() > 0:
            for key in store:
                print("\tid=0, obj=1".format(key, self.root.ids[key]))
                if isinstance(self.root.ids[key], MDTextField):
                    self.root.ids[key].text = store.get(key)['text']
                    print("\t\ttext=", self.root.ids[key].text)

    def on_stop(self):
        print("\non_stop:")
        store = JsonStore('myBackup.json')
        for key in self.root.ids:
            if isinstance(self.root.ids[key], MDTextField):
                print("\tid=0, text=1".format(key, self.root.ids[key].text))
                store.put(key, text=self.root.ids[key].text)


if __name__ == "__main__":
    TestApp().run()

test.kv

#:kivy 1.11.0
#:import MDTextField kivymd.textfields.MDTextField

<RootWidget>:
    MDTextField:
        id: first_text_field
        color: 0, 0, 0, 1    # black text color
        focus: True
        hint_text: "Helper text on focus"
        helper_text: "This will disappear when you click off"
        helper_text_mode: "on_focus"
        input_filter: "int"
        on_text_validate: root.on_text_validate_callback(self)

输出

【讨论】:

加一个给你答案:)。我会尽快尝试并接受您的回答。 我尝试将您的代码实现到我的代码中,但它返回错误。请参阅更新后的帖子。如果您愿意,我还可以发布另一个问题。顺便说一句,您是否(有机会)为 kivy 创建教程视频/教程?你似乎对 kivy 很有经验:)。 第一个问题,AttributeError是因为self.root.ids.first_text_field.text在kv文件中不存在。您需要将first_text_field 替换为controller_parameter_1controller_parameter_2。第二个问题,on_text_validate_callback()方法是在App类中实现的。您需要将root.on_text_validate_callback(self) 替换为app.root.on_text_validate_callback(self)。请参考更新的代码来解决多个 TextInput 小部件。 请使用Window.bind(on_close=self.on_stop)参考更新后的帖子 在 App 类中,使用self.root.ids,因为ids 是在根目录下定义的。当你的 kv 文件被解析时,kivy 会收集所有带有 id 标记的小部件,并将它们放在这个 self.ids 字典类型属性中。您可以使用self.root.ids['time'].textself.root.ids.time.text 引用小部件。请查看Accessing Widgets defined inside Kv lang in your python code 文档。

以上是关于Python kivy(kivymd)如何从MD存储和加载数据的主要内容,如果未能解决你的问题,请参考以下文章

如果我在另一个屏幕中,我如何获取屏幕的 MDTextField(或数据“登录”)的文本。 (基维/基维MD/Python)

如何通过按下按钮 kivy/kivymd/python 在另一个窗口中创建按钮

如何在 Kivy 或 KivyMD 中创建类似快照的按钮?

Kivymd 卡按钮功能

如何从for循环中获取用户输入的数据到kivymd中的python文件

第一个 Python/Kivy/KivyMD 应用程序。无法将文本字段中的数据保存到变量 + 数据绑定到 MDList