如何在 kivy python 中保存文本输入?
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'}
您无需对文本更改进行操作,仅当单击 'Run' 按钮时。因此,将 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]
我正在用 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'}
您无需对文本更改进行操作,仅当单击 'Run' 按钮时。因此,将 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]