简体   繁体   中英

How to save the textinput in kivy python?

I'm doing an app with kivy and I have a problem about saving the information of the texts inputs. As you can see in the code, the program has 2 screen: the first is a selection of the fields that will be shown in the second screen, and the second has a single input for each field selected in the main screen. The problem is that I want to print the inputs when the button Run is pressed in the second screen, and I don't know how to do it. I have been told that maybe with a ListProperty I could save all the inputs, but I've tried many times and doesn't work.

# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.properties import DictProperty

Builder.load_string('''
<Root>:
    MainScreen:
        name: 'main'
    AnotherScreen:
        name: 'another'

<MainScreen>:
    GridLayout:
        cols: 2
        Label:
            text: "Select Subjects"
            font_size: 15
        Label:
            text: " "
        CheckBox:
            on_active:root.ping('240031',self.active)
        Label:
            text: "Electromagnetisme"
        CheckBox:
            on_active:root.ping('240033',self.active)
        Label:
            text: "Materials"
        CheckBox:
            on_active:root.ping('240052',self.active)
        Label:
            text: "Termodinàmica"
        CheckBox:
            on_active:root.ping('240053',self.active)
        Label:
            text: "Electrotècnia"
        CheckBox:
            on_active:root.ping('240054',self.active)
        Label:
            text: "Mecànica dels Medis Continus"
        CheckBox:
            on_active:root.ping('240061',self.active)
        Label:
            text: "Mecànica de Fluids"
        CheckBox:
            on_active:root.ping('240063',self.active)
        Label:
            text: "Resistència de Materials"
        CheckBox:
            on_active:root.ping('240072',self.active)
        Label:
            text: "Electrònica"
        CheckBox:
            on_active:root.ping('240073',self.active)
        Label:
            text: "Sistemes de Fabricació"
        CheckBox:
            on_active:root.ping('240151',self.active)
        Label:
            text: "Tecnologia i Selecció de Materials"
        CheckBox:
            on_active:root.ping('240161',self.active)
        Label:
            text: "Màquines Elèctriques"
        CheckBox:
            on_active:root.ping('240171',self.active)
        Label:
            text: "Termotècnia"
        CheckBox:
            on_active:root.ping('240172',self.active)
        Label:
            text: "Control Automàtic"

        Button:
            text: "Exit"
            background_color: .7, 1, 6, 1
            on_release:root.parent.current='another'
        Button:
            text: "Run"
            font_size: 
            background_color: .7, .7, 1, 1
            on_release: root.parent.current='another'

<AnotherScreen>:
    GridLayout:
        id: container
        cols: 2

''')


 class MainScreen(Screen):
    def __init__(self, **kw):
        super(MainScreen, self).__init__(**kw)
        self.a = App.get_running_app()
    def ping(self, n, value):
        self.a.big_dict[n] = value

class AnotherScreen(Screen):
    def on_pre_enter(self, *args):
        a = App.get_running_app()
        t=[]
        self.ids.container.add_widget(Label(markup=True,text="[b]Name[/b]",background_color=[0,1,1,1]))
        self.ids.container.add_widget(Label(markup=True,background_color=[0,1,1,1],text="[b]Insert Data[/b]"))
        def add(self,p):
            t.append(p)
        for k,v in a.big_dict.iteritems():
            if v:
                e=k
                self.ids.container.add_widget(Label(text=k))
                self.k=TextInput(id=k,multiline=False)
                self.k.bind(text=add)
                self.ids.container.add_widget(self.k)

        def run(self):
            print t
        b1=Button(text='Exit',background_color=[0,1,0,1])
        self.ids.container.add_widget(b1)
        b2=Button(text='Run',background_color=[0,1,0,1])
        self.ids.container.add_widget(b2)
        b1.bind(on_release=exit)
        b2.bind(on_release=run)



class Root(ScreenManager):
    pass
class SimpleKivy(App):
    big_dict = DictProperty({'240161': False, '240061': False, '240171': False, '240151': False, '240063': False, '240073': False, '240072': False, '240031': False, '240033': False, '240054': False, '240053': False, '240052': False, '240172': False})
    def build(self):
        return Root()
SimpleKivy().run()

If anyone knows how to save the information that is inserted in the textinput boxes, please comment because I've already tried a lot of things and I don't find the error.

The issue is that you're appending to the list every time text changes. If you use a dict instead you'll get the desired result:

    t={}
    self.ids.container.add_widget(Label(markup=True,text="[b]Name[/b]",background_color=[0,1,1,1]))
    self.ids.container.add_widget(Label(markup=True,background_color=[0,1,1,1],text="[b]Insert Data[/b]"))
    def add(self,p):
        t[self.id] = p

Which will then print something like the following:

{'240031': u'aa', '240033': u'bb', '240052': u'cc'}

You don't need to act on text changes, only when the 'Run' button is clicked. So, store the TextEdits in a property:

class AnotherScreen(Screen):
    tes = ListProperty()

...

        for k,v in a.big_dict.iteritems():
            if v:
                self.ids.container.add_widget(Label(text=k))
                te = TextInput(id=k,multiline=False)
                self.tes.append(te)
                self.ids.container.add_widget(te)
...
        b2.bind(on_release=self.run)

....
    def run(self, instance):
        print [t.text for t in self.tes]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM