Android 如何使用kivy从TextInput写入并保存到文本文件
我想在TextInput小部件中输入文本,将其保存到文本文件中。请有人告诉我一个例子,如何获得在TextInput小部件中输入的值,以将其保存到文本文件中Android 如何使用kivy从TextInput写入并保存到文本文件,android,python,kivy,Android,Python,Kivy,我想在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 imp
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保存(self,nam):
fob=open('c:/test.txt','w')
write=离岸价write(str(name))
Builder.load_字符串(“”)
:
盒子布局:
按钮:
文本:“添加新员工”
按:root.manager.current='add\u staff'
按钮:
文本:“查看员工档案”
按钮:
文本:“工资报告”
:
nam:str(名称输入)
作业:作业输入
网格布局:
科尔斯:2
标签:
文本:“名称”
文本输入:
id:name\u输入
多行:False
标签:
文字:“工作”
文本输入:
id:job\u输入
标签:
文本:“工资”
文本输入:
标签:
正文:“加入日期”
文本输入:
按钮:
文本:“返回菜单”
按:root.manager.current='菜单'
按钮:
文本:“保存”
按:应用程序保存(self,nam)
''')
类菜单屏幕(屏幕):
通过
班级添加新员工(屏幕):
通过
sm=屏幕管理器()
sm.add_小部件(菜单浏览(name='menu'))
sm.add_小部件(add_new_staff(name='add_staff'))
类TestApp(应用程序):
def生成(自):
返回sm
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
TestApp().run()
尝试创建文件夹2c:\test
并保存到该位置,而不是C:\
class TestApp(App):
def build(self):
return sm
def save(self,nam):
fob = open('c:/test/test.txt','w')
write = fob.write(str(name))
将防止您提到的错误这里是您的工作示例
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
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(name_input.text, job_input.text)
''')
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
def save(self, name, job):
fob = open('c:/test.txt','w')
fob.write(name + "\n")
fob.write(job)
fob.close()
if __name__ == '__main__':
TestApp().run()
从kivy.app导入应用
从kivy.lang导入生成器
从kivy.uix.screenmanager导入screenmanager,屏幕
从kivy.uix.label导入标签
从kivy.uix.textinput导入textinput
Builder.load_字符串(“”)
:
盒子布局:
按钮:
文本:“添加新员工”
按:root.manager.current='add\u staff'
按钮:
文本:“查看员工档案”
按钮:
文本:“工资报告”
希望有帮助
干杯当我运行程序并从add_new_staff添加详细信息,然后按save按钮时,它显示“TestApp”对象没有“save”属性。实际上我是kivy的新手。请给出一个示例,如何获取在“TextInput”小部件中输入的值并将其保存到文本文件中。@ZenCODE Hi!我收到此错误IOError:[Errno 13]权限被拒绝:“c:/test.txt”
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
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(name_input.text, job_input.text)
''')
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
def save(self, name, job):
fob = open('c:/test.txt','w')
fob.write(name + "\n")
fob.write(job)
fob.close()
if __name__ == '__main__':
TestApp().run()