我正在用kivy做一個應用程序,我對保存文本輸入信息存在問題。正如你在代碼中看到的那樣,程序有兩個屏幕:第一個是在第二個屏幕上顯示的字段的選擇,第二個對於在主屏幕中選擇的每個字段具有單個輸入。問題是我想在第二個屏幕按下按鈕Run時打印輸入,但我不知道該怎麼做。我被告知可能用ListProperty
我可以保存所有的輸入,但我已經嘗試了很多次,不起作用。如何在kivy python中保存textinput?
# -*- 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
框中的信息,請評論,因爲我已經嘗試了很多的東西,我沒有找到錯誤。