将复选框数据和文本输入数据保存到.Json文件

时间:2019-05-12 07:34:45

标签: python python-3.x kivy

我正在尝试将应用程序页面中的数据保存到.json文件中。有2个复选框和1个文本输入区域。我只希望保存一个或另一个,因为并非全部都需要保存。只是选中的一个。

我已经尝试阅读kivy文档,并且ive从这里和网络上尝试了各种示例,但我仍然无法按预期将数据保存到json文件中。


from kivy.app import App
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.properties import ListProperty, ObjectProperty
from kivy.uix.checkbox import CheckBox
from kivy.uix.button import Button
from kivy.storage.jsonstore import JsonStore
from kivy.clock import Clock

class MyScreenManager(ScreenManager):
    pass

class FirstScreen(Screen):
    pass

class Who(Screen):
    def __init__(self, **kwargs):
        super(Who, self).__init__(**kwargs)
        self.store = JsonStore("who.json")
        Clock.schedule_once(lambda *args: self.load())

    def save(self):
        self.store.put('who',
            mum = self.mum.checkbox,
            dad = self.dad.checkbox,
            other = self.other.text)

    def load(self):
        if self.store.exists('who'):
            who = self.store.get('who')
            w = [("mum", self.mum), ("dad", self.dad), ("other", self.other)]
            for key, ti in v:
                val = who.get(key)
                if val:
                    ti.text = val

root_widget = Builder.load_file("test.kv")

runTouchApp(root_widget)

我的kv文档如下所示:


#:import FadeTransition kivy.uix.screenmanager.FadeTransition
#:import StringProperty kivy.properties.StringProperty
#:import ObjectProperty kivy.properties.ObjectProperty
#:import Label kivy.uix.label.Label
#:import Window kivy.core.window.Window
#:import ListProperty kivy.properties.ListProperty
#:import CheckBox kivy.uix.checkbox.CheckBox
#:import TextInput kivy.uix.textinput.TextInput
#:import Button kivy.uix.button.Button
#:import BoxLayout kivy.uix.boxlayout.BoxLayout
#:import GridLayout kivy.uix.gridlayout.GridLayout
#:import CheckBox kivy.uix.checkbox.CheckBox

MyScreenManager:

    transition: FadeTransition()
    Who:
    FirstScreen:

<Who>:
    name: 'who'   

    mum: mum
    dad: dad
    other: other

    GridLayout:
        cols: 2
        row_force_default: True
        row_default_height: '50dp'

        Label:
            text: 'Mum'
            font_size: 30

        CheckBox:
            id: mum
            on_press: root.save(mum.active, mum.text)

        Label:
            text: 'Dad'
            font_size: 30

        CheckBox:
            id: dad
            on_press: root.save(dad.active, dad.text)

        Label:
            text: 'Someone Else'
            font_size: 30

        TextInput:
            id: other
            multiline: False
            font_size: 30

        Button:
            text: 'Book'
            font_size: 30
            valign: 'middle'
            on_press: root.save()

        Button:
            text: 'Cancel'
            font_size: 30
            valign: 'middle'
            on_press: root.manager.current = 'first'

<FirstScreen>:
    name: 'first'

    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'Go Back'
            font_size: 40
        BoxLayout:
            Button:
                text: 'Back To Who'
                font_size: 40
                on_press: root.manager.current = 'who'

结果通常围绕此:

在kivy.weakproxy.WeakProxy中的文件“ kivy / weakproxy.pyx”,第30行。 getattr  AttributeError:'CheckBox'对象没有属性'text'


预期结果显然是保存的json数据。我需要能够一次选择一个或另一个,甚至全部选择。我已经尽力了。我的猫怕我。我需要帮助。谢谢。

0 个答案:

没有答案