【发布时间】:2014-02-13 19:30:21
【问题描述】:
我想在 TextInput 小部件中输入文本以将其保存到文本文件中。请有人向我展示一个示例,如何获取在 TextInput 小部件中输入的值以将其保存到文本文件中。
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
import os
此方法尝试保存到文本文件中,但没有成功
def save(self,nam):
fob = open('c:/test.txt','w')
write = fob.write(str(name))
Builder.load_string('''
<MenuScreen>:
BoxLayout:
Button:
text: 'Add New Staff'
on_press: root.manager.current = 'add_staff'
Button:
text: 'View Staff Profile'
Button:
text: 'Salary report'
<Add_new_staff>:
nam: str(name_input)
job: job_input
GridLayout:
cols: 2
Label:
text: 'Name'
TextInput:
id: name_input
multiline: False
Label:
text: 'Job'
TextInput:
id: job_input
Label:
text: 'Salary'
TextInput:
Label:
text: 'Date of Joining'
TextInput:
Button:
text: 'Back to menu'
on_press: root.manager.current = 'menu'
Button:
text: 'Save'
on_press: app.save(self,nam)
''')
class MenuScreen(Screen):
pass
class Add_new_staff(Screen):
pass
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(Add_new_staff(name='add_staff'))
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
【问题讨论】:
-
当我运行程序并从 Add_new_staff 添加详细信息,然后按保存按钮时,它说“TestApp”对象没有“保存”属性。实际上我是 kivy 的新手。
-
请举例说明如何获取“TextInput”小部件中输入的值并将其保存到文本文件中。