【问题标题】:How to save the textinput in kivy python?如何在kivy python中保存文本输入?
【发布时间】:2016-03-17 18:16:49
【问题描述】:

我正在使用 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()

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

【问题讨论】:

    标签: python checkbox kivy textinput


    【解决方案1】:

    您无需对文本更改进行操作,只需单击“运行”按钮即可。因此,将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]
    

    【讨论】:

      【解决方案2】:

      问题是您每次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'}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-01
        • 1970-01-01
        • 2022-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多