
我想在Textinput小部件中输入文本以将其保存到文本文件中.请有人给我展示一个示例,该示例如何获取在Textinput小部件中输入的值以将其保存到文本文件中.
from kivy.app import Appfrom kivy.lang import Builderfrom kivy.uix.screenmanager import ScreenManager, Screenfrom kivy.uix.label import Labelfrom kivy.uix.textinput import Textinputimport 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): passclass Add_new_staff(Screen): passsm = ScreenManager()sm.add_Widget(MenuScreen(name='menu'))sm.add_Widget(Add_new_staff(name='add_staff'))class TestApp(App): def build(self): return smif __name__ == '__main__': TestApp().run()解决方法:
这是您的示例工作.
from kivy.app import Appfrom kivy.lang import Builderfrom kivy.uix.screenmanager import ScreenManager, Screenfrom kivy.uix.label import Labelfrom kivy.uix.textinput import TextinputBuilder.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): passclass Add_new_staff(Screen): passsm = 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()但是有几点建议.
1.而是使用数据库(sqlite3?)存储此类数据.它将更有效地扩展,在数据变大时为您提供更快的查找.
2.将您的数据存储在所有用户的“读/写”位置. Kivy为此提供了便利功能.
http://kivy.org/docs/api-kivy.app.html?highlight=data_dir#kivy.app.App.user_data_dir
希望有帮助吗?
干杯
总结以上是内存溢出为你收集整理的android-如何使用kivy从TextInput写入并保存到文本文件全部内容,希望文章能够帮你解决android-如何使用kivy从TextInput写入并保存到文本文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)