繁体   English   中英

如何在kivy python中保存textinput?

[英]How to save the textinput in kivy python?

我正在使用kivy做一个应用程序,但是在保存文本输入信息时遇到问题。 正如您在代码中看到的那样,该程序有2个屏幕:第一个是将在第二个屏幕中显示的字段的选择,第二个是在主屏幕中选择的每个字段的单个输入。 问题是我想在第二个屏幕中按下“运行”按钮时打印输入,但我不知道该怎么做。 有人告诉我,也许可以使用ListProperty保存所有输入,但是我已经尝试了很多次,但是没有用。

# -*- 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()

如果有人知道如何保存在textinput框中插入的信息,请发表评论,因为我已经尝试了很多事情,但没有发现错误。

问题是,每次text更改时,您都要追加到列表中。 如果使用dict ,则会得到所需的结果:

    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

然后将打印如下内容:

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

仅在单击“运行”按钮时,才需要对文本更改进行操作。 因此,将TextEdits存储在一个属性中:

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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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